* Integration for Energenie Power-Strips (EGPS) * cleanups reocommended by reviewer * Adds missing exception handling when trying to send a command to an unreachable device. * fix: incorrect handling of already opened devices in pyegps api. bump to pyegps=0.2.4 * Add blank line after file docstring, and other cosmetics * change asyncio.to_thread to async_add_executer_job * raises HomeAssistantError EgpsException in switch services. * switch test parameterized by entity name * reoved unused device registry * add translation_key and update_before_add * bump pyegps dependency to version to 0.2.5 * combined get_device patches and put into conftest.py * changed switch entity to use _attr_is_on and cleanups * further cleanup * Apply suggestions from code review * refactor: rename egps to energenie_power_sockets * updated test snapshot --------- Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
"""Switch implementation for Energenie-Power-Sockets Platform."""
|
|
|
|
from typing import Any
|
|
|
|
from pyegps import __version__ as PYEGPS_VERSION
|
|
from pyegps.exceptions import EgpsException
|
|
from pyegps.powerstrip import PowerStrip
|
|
|
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Add EGPS sockets for passed config_entry in HA."""
|
|
powerstrip: PowerStrip = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
async_add_entities(
|
|
(
|
|
EGPowerStripSocket(powerstrip, socket)
|
|
for socket in range(powerstrip.numberOfSockets)
|
|
),
|
|
update_before_add=True,
|
|
)
|
|
|
|
|
|
class EGPowerStripSocket(SwitchEntity):
|
|
"""Represents a socket of an Energenie-Socket-Strip."""
|
|
|
|
_attr_device_class = SwitchDeviceClass.OUTLET
|
|
_attr_has_entity_name = True
|
|
_attr_translation_key = "socket"
|
|
|
|
def __init__(self, dev: PowerStrip, socket: int) -> None:
|
|
"""Initiate a new socket."""
|
|
self._dev = dev
|
|
self._socket = socket
|
|
self._attr_translation_placeholders = {"socket_id": str(socket)}
|
|
|
|
self._attr_unique_id = f"{dev.device_id}_{socket}"
|
|
self._attr_device_info = DeviceInfo(
|
|
identifiers={(DOMAIN, dev.device_id)},
|
|
name=dev.name,
|
|
manufacturer=dev.manufacturer,
|
|
model=dev.name,
|
|
sw_version=PYEGPS_VERSION,
|
|
)
|
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
|
"""Switch the socket on."""
|
|
try:
|
|
self._dev.switch_on(self._socket)
|
|
except EgpsException as err:
|
|
raise HomeAssistantError(f"Couldn't access USB device: {err}") from err
|
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
|
"""Switch the socket off."""
|
|
try:
|
|
self._dev.switch_off(self._socket)
|
|
except EgpsException as err:
|
|
raise HomeAssistantError(f"Couldn't access USB device: {err}") from err
|
|
|
|
def update(self) -> None:
|
|
"""Read the current state from the device."""
|
|
try:
|
|
self._attr_is_on = self._dev.get_status(self._socket)
|
|
except EgpsException as err:
|
|
raise HomeAssistantError(f"Couldn't access USB device: {err}") from err
|