From 5ef3901b440503941f73b1e71df718e3c9b2d60c Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Sun, 22 Dec 2024 13:32:15 +0100 Subject: [PATCH] Add base entity for Niko Home Control (#133744) --- .../components/niko_home_control/entity.py | 50 +++++++++++++++++++ .../components/niko_home_control/light.py | 25 +++------- 2 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 homeassistant/components/niko_home_control/entity.py diff --git a/homeassistant/components/niko_home_control/entity.py b/homeassistant/components/niko_home_control/entity.py new file mode 100644 index 00000000000000..fe14e09d957109 --- /dev/null +++ b/homeassistant/components/niko_home_control/entity.py @@ -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.""" diff --git a/homeassistant/components/niko_home_control/light.py b/homeassistant/components/niko_home_control/light.py index 29b952fcb772a7..c9902cbf11b94d 100644 --- a/homeassistant/components/niko_home_control/light.py +++ b/homeassistant/components/niko_home_control/light.py @@ -25,6 +25,7 @@ from . import NHCController, NikoHomeControlConfigEntry from .const import DOMAIN +from .entity import NikoHomeControlEntity # delete after 2025.7.0 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.""" + _attr_name = None + _action: NHCLight + def __init__( self, action: NHCLight, controller: NHCController, unique_id: str ) -> None: """Set up the Niko Home Control light platform.""" - self._controller = controller - self._action = action - self._attr_unique_id = f"{unique_id}-{action.id}" - self._attr_name = action.name - self._attr_is_on = action.is_on + super().__init__(action, controller, unique_id) self._attr_color_mode = ColorMode.ONOFF self._attr_supported_color_modes = {ColorMode.ONOFF} - self._attr_should_poll = False if action.is_dimmable: self._attr_color_mode = 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: """Instruct the light to turn on.""" self._action.turn_on(kwargs.get(ATTR_BRIGHTNESS, 255) / 2.55) @@ -126,9 +117,9 @@ def turn_off(self, **kwargs: Any) -> None: """Instruct the light to 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.""" + state = self._action.state self._attr_is_on = state > 0 if brightness_supported(self.supported_color_modes): self._attr_brightness = round(state * 2.55) - self.async_write_ha_state()