mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +00:00
Move wilight base entity to separate module (#126193)
This commit is contained in:
parent
3d9aa60e4e
commit
989a90bb93
@ -1,20 +1,13 @@
|
|||||||
"""The WiLight integration."""
|
"""The WiLight integration."""
|
||||||
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from pywilight.wilight_device import PyWiLightDevice
|
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
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
|
from .parent_device import WiLightParent
|
||||||
|
|
||||||
DOMAIN = "wilight"
|
|
||||||
|
|
||||||
# List the platforms that you want to support.
|
# List the platforms that you want to support.
|
||||||
PLATFORMS = [Platform.COVER, Platform.FAN, Platform.LIGHT, Platform.SWITCH]
|
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]
|
del hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
return unload_ok
|
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)
|
|
||||||
|
@ -9,7 +9,7 @@ from homeassistant.components import ssdp
|
|||||||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_HOST
|
from homeassistant.const import CONF_HOST
|
||||||
|
|
||||||
from . import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
CONF_SERIAL_NUMBER = "serial_number"
|
CONF_SERIAL_NUMBER = "serial_number"
|
||||||
CONF_MODEL_NAME = "model_name"
|
CONF_MODEL_NAME = "model_name"
|
||||||
|
3
homeassistant/components/wilight/const.py
Normal file
3
homeassistant/components/wilight/const.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
"""The WiLight integration."""
|
||||||
|
|
||||||
|
DOMAIN = "wilight"
|
@ -20,7 +20,8 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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 .parent_device import WiLightParent
|
||||||
|
|
||||||
|
|
||||||
|
59
homeassistant/components/wilight/entity.py
Normal file
59
homeassistant/components/wilight/entity.py
Normal 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)
|
@ -25,7 +25,8 @@ from homeassistant.util.percentage import (
|
|||||||
percentage_to_ordered_list_item,
|
percentage_to_ordered_list_item,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import DOMAIN, WiLightDevice
|
from .const import DOMAIN
|
||||||
|
from .entity import WiLightDevice
|
||||||
from .parent_device import WiLightParent
|
from .parent_device import WiLightParent
|
||||||
|
|
||||||
ORDERED_NAMED_FAN_SPEEDS = [WL_SPEED_LOW, WL_SPEED_MEDIUM, WL_SPEED_HIGH]
|
ORDERED_NAMED_FAN_SPEEDS = [WL_SPEED_LOW, WL_SPEED_MEDIUM, WL_SPEED_HIGH]
|
||||||
|
@ -17,7 +17,8 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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 .parent_device import WiLightParent
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,7 +14,8 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
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 .parent_device import WiLightParent
|
||||||
from .support import wilight_to_hass_trigger, wilight_trigger as wl_trigger
|
from .support import wilight_to_hass_trigger, wilight_trigger as wl_trigger
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user