From 989a90bb93ed84b19ccf403f866ff57395106b3f Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 18 Sep 2024 10:56:39 +0200 Subject: [PATCH] Move wilight base entity to separate module (#126193) --- homeassistant/components/wilight/__init__.py | 59 +------------------ .../components/wilight/config_flow.py | 2 +- homeassistant/components/wilight/const.py | 3 + homeassistant/components/wilight/cover.py | 3 +- homeassistant/components/wilight/entity.py | 59 +++++++++++++++++++ homeassistant/components/wilight/fan.py | 3 +- homeassistant/components/wilight/light.py | 3 +- homeassistant/components/wilight/switch.py | 3 +- 8 files changed, 73 insertions(+), 62 deletions(-) create mode 100644 homeassistant/components/wilight/const.py create mode 100644 homeassistant/components/wilight/entity.py diff --git a/homeassistant/components/wilight/__init__.py b/homeassistant/components/wilight/__init__.py index 067197c8a14..5242f84ab93 100644 --- a/homeassistant/components/wilight/__init__.py +++ b/homeassistant/components/wilight/__init__.py @@ -1,20 +1,13 @@ """The WiLight integration.""" -from typing import Any - -from pywilight.wilight_device import PyWiLightDevice - from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.entity import Entity +from .const import DOMAIN from .parent_device import WiLightParent -DOMAIN = "wilight" - # List the platforms that you want to support. PLATFORMS = [Platform.COVER, Platform.FAN, Platform.LIGHT, Platform.SWITCH] @@ -48,51 +41,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: del hass.data[DOMAIN][entry.entry_id] return unload_ok - - -class WiLightDevice(Entity): - """Representation of a WiLight device. - - Contains the common logic for WiLight entities. - """ - - _attr_should_poll = False - _attr_has_entity_name = True - - def __init__(self, api_device: PyWiLightDevice, index: str, item_name: str) -> None: - """Initialize the device.""" - # WiLight specific attributes for every component type - self._device_id = api_device.device_id - self._client = api_device.client - self._index = index - self._status: dict[str, Any] = {} - - self._attr_unique_id = f"{self._device_id}_{index}" - self._attr_device_info = DeviceInfo( - name=item_name, - identifiers={(DOMAIN, self._attr_unique_id)}, - model=api_device.model, - manufacturer="WiLight", - sw_version=api_device.swversion, - via_device=(DOMAIN, self._device_id), - ) - - @property - def available(self) -> bool: - """Return True if entity is available.""" - return bool(self._client.is_connected) - - @callback - def handle_event_callback(self, states: dict[str, Any]) -> None: - """Propagate changes through ha.""" - self._status = states - self.async_write_ha_state() - - async def async_update(self) -> None: - """Synchronize state with api_device.""" - await self._client.status(self._index) - - async def async_added_to_hass(self) -> None: - """Register update callback.""" - self._client.register_status_callback(self.handle_event_callback, self._index) - await self._client.status(self._index) diff --git a/homeassistant/components/wilight/config_flow.py b/homeassistant/components/wilight/config_flow.py index 8795da19091..b7f9b9485ed 100644 --- a/homeassistant/components/wilight/config_flow.py +++ b/homeassistant/components/wilight/config_flow.py @@ -9,7 +9,7 @@ from homeassistant.components import ssdp from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import CONF_HOST -from . import DOMAIN +from .const import DOMAIN CONF_SERIAL_NUMBER = "serial_number" CONF_MODEL_NAME = "model_name" diff --git a/homeassistant/components/wilight/const.py b/homeassistant/components/wilight/const.py new file mode 100644 index 00000000000..29de5093b70 --- /dev/null +++ b/homeassistant/components/wilight/const.py @@ -0,0 +1,3 @@ +"""The WiLight integration.""" + +DOMAIN = "wilight" diff --git a/homeassistant/components/wilight/cover.py b/homeassistant/components/wilight/cover.py index 4ae4692db40..8a5cb45d909 100644 --- a/homeassistant/components/wilight/cover.py +++ b/homeassistant/components/wilight/cover.py @@ -20,7 +20,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import DOMAIN, WiLightDevice +from .const import DOMAIN +from .entity import WiLightDevice from .parent_device import WiLightParent diff --git a/homeassistant/components/wilight/entity.py b/homeassistant/components/wilight/entity.py new file mode 100644 index 00000000000..b8edf44b495 --- /dev/null +++ b/homeassistant/components/wilight/entity.py @@ -0,0 +1,59 @@ +"""The WiLight integration.""" + +from typing import Any + +from pywilight.wilight_device import PyWiLightDevice + +from homeassistant.core import callback +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.entity import Entity + +from .const import DOMAIN + + +class WiLightDevice(Entity): + """Representation of a WiLight device. + + Contains the common logic for WiLight entities. + """ + + _attr_should_poll = False + _attr_has_entity_name = True + + def __init__(self, api_device: PyWiLightDevice, index: str, item_name: str) -> None: + """Initialize the device.""" + # WiLight specific attributes for every component type + self._device_id = api_device.device_id + self._client = api_device.client + self._index = index + self._status: dict[str, Any] = {} + + self._attr_unique_id = f"{self._device_id}_{index}" + self._attr_device_info = DeviceInfo( + name=item_name, + identifiers={(DOMAIN, self._attr_unique_id)}, + model=api_device.model, + manufacturer="WiLight", + sw_version=api_device.swversion, + via_device=(DOMAIN, self._device_id), + ) + + @property + def available(self) -> bool: + """Return True if entity is available.""" + return bool(self._client.is_connected) + + @callback + def handle_event_callback(self, states: dict[str, Any]) -> None: + """Propagate changes through ha.""" + self._status = states + self.async_write_ha_state() + + async def async_update(self) -> None: + """Synchronize state with api_device.""" + await self._client.status(self._index) + + async def async_added_to_hass(self) -> None: + """Register update callback.""" + self._client.register_status_callback(self.handle_event_callback, self._index) + await self._client.status(self._index) diff --git a/homeassistant/components/wilight/fan.py b/homeassistant/components/wilight/fan.py index 71559658c35..71f1098603b 100644 --- a/homeassistant/components/wilight/fan.py +++ b/homeassistant/components/wilight/fan.py @@ -25,7 +25,8 @@ from homeassistant.util.percentage import ( percentage_to_ordered_list_item, ) -from . import DOMAIN, WiLightDevice +from .const import DOMAIN +from .entity import WiLightDevice from .parent_device import WiLightParent ORDERED_NAMED_FAN_SPEEDS = [WL_SPEED_LOW, WL_SPEED_MEDIUM, WL_SPEED_HIGH] diff --git a/homeassistant/components/wilight/light.py b/homeassistant/components/wilight/light.py index 1a51ecd884e..fbe2499798d 100644 --- a/homeassistant/components/wilight/light.py +++ b/homeassistant/components/wilight/light.py @@ -17,7 +17,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import DOMAIN, WiLightDevice +from .const import DOMAIN +from .entity import WiLightDevice from .parent_device import WiLightParent diff --git a/homeassistant/components/wilight/switch.py b/homeassistant/components/wilight/switch.py index 94e39492626..f2a1ce8b0c5 100644 --- a/homeassistant/components/wilight/switch.py +++ b/homeassistant/components/wilight/switch.py @@ -14,7 +14,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_platform from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import DOMAIN, WiLightDevice +from .const import DOMAIN +from .entity import WiLightDevice from .parent_device import WiLightParent from .support import wilight_to_hass_trigger, wilight_trigger as wl_trigger