From 861b694cffbeb0813dc75320924307cfea3acfde Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 5 Aug 2022 11:39:51 +0200 Subject: [PATCH] Use attributes in litejet light (#76031) --- homeassistant/components/litejet/light.py | 67 ++++++++--------------- 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/homeassistant/components/litejet/light.py b/homeassistant/components/litejet/light.py index 74395117c46..a41a34016d9 100644 --- a/homeassistant/components/litejet/light.py +++ b/homeassistant/components/litejet/light.py @@ -1,7 +1,11 @@ """Support for LiteJet lights.""" +from __future__ import annotations + import logging from typing import Any +from pylitejet import LiteJet + from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_TRANSITION, @@ -27,13 +31,13 @@ async def async_setup_entry( ) -> None: """Set up entry.""" - system = hass.data[DOMAIN] + system: LiteJet = hass.data[DOMAIN] - def get_entities(system): + def get_entities(system: LiteJet) -> list[LiteJetLight]: entities = [] - for i in system.loads(): - name = system.get_load_name(i) - entities.append(LiteJetLight(config_entry, system, i, name)) + for index in system.loads(): + name = system.get_load_name(index) + entities.append(LiteJetLight(config_entry, system, index, name)) return entities async_add_entities(await hass.async_add_executor_job(get_entities, system), True) @@ -43,16 +47,22 @@ class LiteJetLight(LightEntity): """Representation of a single LiteJet light.""" _attr_color_mode = ColorMode.BRIGHTNESS + _attr_should_poll = False _attr_supported_color_modes = {ColorMode.BRIGHTNESS} _attr_supported_features = LightEntityFeature.TRANSITION - def __init__(self, config_entry, lj, i, name): # pylint: disable=invalid-name + def __init__( + self, config_entry: ConfigEntry, litejet: LiteJet, index: int, name: str + ) -> None: """Initialize a LiteJet light.""" self._config_entry = config_entry - self._lj = lj - self._index = i - self._brightness = 0 - self._name = name + self._lj = litejet + self._index = index + self._attr_brightness = 0 + self._attr_is_on = False + self._attr_name = name + self._attr_unique_id = f"{config_entry.entry_id}_{index}" + self._attr_extra_state_attributes = {ATTR_NUMBER: self._index} async def async_added_to_hass(self) -> None: """Run when this Entity has been added to HA.""" @@ -63,41 +73,11 @@ class LiteJetLight(LightEntity): """Entity being removed from hass.""" self._lj.unsubscribe(self._on_load_changed) - def _on_load_changed(self): + def _on_load_changed(self) -> None: """Handle state changes.""" - _LOGGER.debug("Updating due to notification for %s", self._name) + _LOGGER.debug("Updating due to notification for %s", self.name) self.schedule_update_ha_state(True) - @property - def name(self): - """Return the light's name.""" - return self._name - - @property - def unique_id(self): - """Return a unique identifier for this light.""" - return f"{self._config_entry.entry_id}_{self._index}" - - @property - def brightness(self): - """Return the light's brightness.""" - return self._brightness - - @property - def is_on(self): - """Return if the light is on.""" - return self._brightness != 0 - - @property - def should_poll(self): - """Return that lights do not require polling.""" - return False - - @property - def extra_state_attributes(self): - """Return the device state attributes.""" - return {ATTR_NUMBER: self._index} - def turn_on(self, **kwargs: Any) -> None: """Turn on the light.""" @@ -129,4 +109,5 @@ class LiteJetLight(LightEntity): def update(self) -> None: """Retrieve the light's brightness from the LiteJet system.""" - self._brightness = int(self._lj.get_load_level(self._index) / 99 * 255) + self._attr_brightness = int(self._lj.get_load_level(self._index) / 99 * 255) + self._attr_is_on = self.brightness != 0