Skip to content

Commit

Permalink
Add base entity for Niko Home Control (#133744)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Dec 22, 2024
1 parent 56b58ce commit 5ef3901
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 17 deletions.
50 changes: 50 additions & 0 deletions homeassistant/components/niko_home_control/entity.py
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 17 deletions homeassistant/components/niko_home_control/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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)
Expand All @@ -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()

0 comments on commit 5ef3901

Please sign in to comment.