Move wilight base entity to separate module (#126193)

This commit is contained in:
epenet 2024-09-18 10:56:39 +02:00 committed by GitHub
parent 3d9aa60e4e
commit 989a90bb93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 73 additions and 62 deletions

View file

@ -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)

View file

@ -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"

View file

@ -0,0 +1,3 @@
"""The WiLight integration."""
DOMAIN = "wilight"

View file

@ -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

View file

@ -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)

View file

@ -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]

View file

@ -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

View file

@ -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