mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Add base entity for Niko Home Control (#133744)
This commit is contained in:
parent
56b58cec3e
commit
5ef3901b44
50
homeassistant/components/niko_home_control/entity.py
Normal file
50
homeassistant/components/niko_home_control/entity.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
"""Base class for Niko Home Control entities."""
|
||||||
|
|
||||||
|
from abc import abstractmethod
|
||||||
|
|
||||||
|
from nhc.action import NHCAction
|
||||||
|
from nhc.controller import NHCController
|
||||||
|
|
||||||
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
class NikoHomeControlEntity(Entity):
|
||||||
|
"""Base class for Niko Home Control entities."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, action: NHCAction, controller: NHCController, unique_id: str
|
||||||
|
) -> None:
|
||||||
|
"""Set up the Niko Home Control entity."""
|
||||||
|
self._controller = controller
|
||||||
|
self._action = action
|
||||||
|
self._attr_unique_id = unique_id = f"{unique_id}-{action.id}"
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, unique_id)},
|
||||||
|
manufacturer="Niko",
|
||||||
|
name=action.name,
|
||||||
|
suggested_area=action.suggested_area,
|
||||||
|
)
|
||||||
|
self.update_state()
|
||||||
|
|
||||||
|
async def async_added_to_hass(self) -> None:
|
||||||
|
"""Subscribe to updates."""
|
||||||
|
self.async_on_remove(
|
||||||
|
self._controller.register_callback(
|
||||||
|
self._action.id, self.async_update_callback
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
async def async_update_callback(self, state: int) -> None:
|
||||||
|
"""Handle updates from the controller."""
|
||||||
|
self.update_state()
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def update_state(self) -> None:
|
||||||
|
"""Update the state of the entity."""
|
@ -25,6 +25,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|||||||
|
|
||||||
from . import NHCController, NikoHomeControlConfigEntry
|
from . import NHCController, NikoHomeControlConfigEntry
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .entity import NikoHomeControlEntity
|
||||||
|
|
||||||
# delete after 2025.7.0
|
# delete after 2025.7.0
|
||||||
PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend({vol.Required(CONF_HOST): cv.string})
|
PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend({vol.Required(CONF_HOST): cv.string})
|
||||||
@ -91,33 +92,23 @@ async def async_setup_entry(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class NikoHomeControlLight(LightEntity):
|
class NikoHomeControlLight(NikoHomeControlEntity, LightEntity):
|
||||||
"""Representation of a Niko Light."""
|
"""Representation of a Niko Light."""
|
||||||
|
|
||||||
|
_attr_name = None
|
||||||
|
_action: NHCLight
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, action: NHCLight, controller: NHCController, unique_id: str
|
self, action: NHCLight, controller: NHCController, unique_id: str
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Niko Home Control light platform."""
|
"""Set up the Niko Home Control light platform."""
|
||||||
self._controller = controller
|
super().__init__(action, controller, unique_id)
|
||||||
self._action = action
|
|
||||||
self._attr_unique_id = f"{unique_id}-{action.id}"
|
|
||||||
self._attr_name = action.name
|
|
||||||
self._attr_is_on = action.is_on
|
|
||||||
self._attr_color_mode = ColorMode.ONOFF
|
self._attr_color_mode = ColorMode.ONOFF
|
||||||
self._attr_supported_color_modes = {ColorMode.ONOFF}
|
self._attr_supported_color_modes = {ColorMode.ONOFF}
|
||||||
self._attr_should_poll = False
|
|
||||||
if action.is_dimmable:
|
if action.is_dimmable:
|
||||||
self._attr_color_mode = ColorMode.BRIGHTNESS
|
self._attr_color_mode = ColorMode.BRIGHTNESS
|
||||||
self._attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
self._attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
|
||||||
"""Subscribe to updates."""
|
|
||||||
self.async_on_remove(
|
|
||||||
self._controller.register_callback(
|
|
||||||
self._action.id, self.async_update_callback
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Instruct the light to turn on."""
|
"""Instruct the light to turn on."""
|
||||||
self._action.turn_on(kwargs.get(ATTR_BRIGHTNESS, 255) / 2.55)
|
self._action.turn_on(kwargs.get(ATTR_BRIGHTNESS, 255) / 2.55)
|
||||||
@ -126,9 +117,9 @@ class NikoHomeControlLight(LightEntity):
|
|||||||
"""Instruct the light to turn off."""
|
"""Instruct the light to turn off."""
|
||||||
self._action.turn_off()
|
self._action.turn_off()
|
||||||
|
|
||||||
async def async_update_callback(self, state: int) -> None:
|
def update_state(self) -> None:
|
||||||
"""Handle updates from the controller."""
|
"""Handle updates from the controller."""
|
||||||
|
state = self._action.state
|
||||||
self._attr_is_on = state > 0
|
self._attr_is_on = state > 0
|
||||||
if brightness_supported(self.supported_color_modes):
|
if brightness_supported(self.supported_color_modes):
|
||||||
self._attr_brightness = round(state * 2.55)
|
self._attr_brightness = round(state * 2.55)
|
||||||
self.async_write_ha_state()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user