From c20d07c14a8ce93b76519c6a928f8936d35c812c Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:44:11 +0200 Subject: [PATCH] Move and rename hlk_sw16 base entity to separate module (#126096) --- homeassistant/components/hlk_sw16/__init__.py | 56 +----------------- homeassistant/components/hlk_sw16/entity.py | 59 +++++++++++++++++++ homeassistant/components/hlk_sw16/switch.py | 5 +- 3 files changed, 63 insertions(+), 57 deletions(-) create mode 100644 homeassistant/components/hlk_sw16/entity.py diff --git a/homeassistant/components/hlk_sw16/__init__.py b/homeassistant/components/hlk_sw16/__init__.py index 3e6a9f6b0d6..ce37be96dcd 100644 --- a/homeassistant/components/hlk_sw16/__init__.py +++ b/homeassistant/components/hlk_sw16/__init__.py @@ -9,11 +9,7 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT, CONF_SWITCHES, Platform from homeassistant.core import HomeAssistant, callback import homeassistant.helpers.config_validation as cv -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.typing import ConfigType from .const import ( @@ -131,53 +127,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if not hass.data[DOMAIN]: hass.data.pop(DOMAIN) return unload_ok - - -class SW16Device(Entity): - """Representation of a HLK-SW16 device. - - Contains the common logic for HLK-SW16 entities. - """ - - _attr_should_poll = False - - def __init__(self, device_port, entry_id, client): - """Initialize the device.""" - # HLK-SW16 specific attributes for every component type - self._entry_id = entry_id - self._device_port = device_port - self._is_on = None - self._client = client - self._attr_name = device_port - self._attr_unique_id = f"{self._entry_id}_{self._device_port}" - - @callback - def handle_event_callback(self, event): - """Propagate changes through ha.""" - _LOGGER.debug("Relay %s new state callback: %r", self.unique_id, event) - self._is_on = event - self.async_write_ha_state() - - @property - def available(self): - """Return True if entity is available.""" - return bool(self._client.is_connected) - - @callback - def _availability_callback(self, availability): - """Update availability state.""" - self.async_write_ha_state() - - async def async_added_to_hass(self): - """Register update callback.""" - self._client.register_status_callback( - self.handle_event_callback, self._device_port - ) - self._is_on = await self._client.status(self._device_port) - self.async_on_remove( - async_dispatcher_connect( - self.hass, - f"hlk_sw16_device_available_{self._entry_id}", - self._availability_callback, - ) - ) diff --git a/homeassistant/components/hlk_sw16/entity.py b/homeassistant/components/hlk_sw16/entity.py new file mode 100644 index 00000000000..fdef5f6764b --- /dev/null +++ b/homeassistant/components/hlk_sw16/entity.py @@ -0,0 +1,59 @@ +"""Support for HLK-SW16 relay switches.""" + +import logging + +from homeassistant.core import callback +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import Entity + +_LOGGER = logging.getLogger(__name__) + + +class SW16Entity(Entity): + """Representation of a HLK-SW16 device. + + Contains the common logic for HLK-SW16 entities. + """ + + _attr_should_poll = False + + def __init__(self, device_port, entry_id, client): + """Initialize the device.""" + # HLK-SW16 specific attributes for every component type + self._entry_id = entry_id + self._device_port = device_port + self._is_on = None + self._client = client + self._attr_name = device_port + self._attr_unique_id = f"{self._entry_id}_{self._device_port}" + + @callback + def handle_event_callback(self, event): + """Propagate changes through ha.""" + _LOGGER.debug("Relay %s new state callback: %r", self.unique_id, event) + self._is_on = event + self.async_write_ha_state() + + @property + def available(self): + """Return True if entity is available.""" + return bool(self._client.is_connected) + + @callback + def _availability_callback(self, availability): + """Update availability state.""" + self.async_write_ha_state() + + async def async_added_to_hass(self): + """Register update callback.""" + self._client.register_status_callback( + self.handle_event_callback, self._device_port + ) + self._is_on = await self._client.status(self._device_port) + self.async_on_remove( + async_dispatcher_connect( + self.hass, + f"hlk_sw16_device_available_{self._entry_id}", + self._availability_callback, + ) + ) diff --git a/homeassistant/components/hlk_sw16/switch.py b/homeassistant/components/hlk_sw16/switch.py index 590ab9c4497..3911dd6eab9 100644 --- a/homeassistant/components/hlk_sw16/switch.py +++ b/homeassistant/components/hlk_sw16/switch.py @@ -7,8 +7,9 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import DATA_DEVICE_REGISTER, SW16Device +from . import DATA_DEVICE_REGISTER from .const import DOMAIN +from .entity import SW16Entity PARALLEL_UPDATES = 0 @@ -31,7 +32,7 @@ async def async_setup_entry( async_add_entities(devices_from_entities(hass, entry)) -class SW16Switch(SW16Device, SwitchEntity): +class SW16Switch(SW16Entity, SwitchEntity): """Representation of a HLK-SW16 switch.""" @property