mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Use attributes in litejet light (#76031)
This commit is contained in:
parent
44aa49dde8
commit
861b694cff
@ -1,7 +1,11 @@
|
|||||||
"""Support for LiteJet lights."""
|
"""Support for LiteJet lights."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from pylitejet import LiteJet
|
||||||
|
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS,
|
ATTR_BRIGHTNESS,
|
||||||
ATTR_TRANSITION,
|
ATTR_TRANSITION,
|
||||||
@ -27,13 +31,13 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up entry."""
|
"""Set up entry."""
|
||||||
|
|
||||||
system = hass.data[DOMAIN]
|
system: LiteJet = hass.data[DOMAIN]
|
||||||
|
|
||||||
def get_entities(system):
|
def get_entities(system: LiteJet) -> list[LiteJetLight]:
|
||||||
entities = []
|
entities = []
|
||||||
for i in system.loads():
|
for index in system.loads():
|
||||||
name = system.get_load_name(i)
|
name = system.get_load_name(index)
|
||||||
entities.append(LiteJetLight(config_entry, system, i, name))
|
entities.append(LiteJetLight(config_entry, system, index, name))
|
||||||
return entities
|
return entities
|
||||||
|
|
||||||
async_add_entities(await hass.async_add_executor_job(get_entities, system), True)
|
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."""
|
"""Representation of a single LiteJet light."""
|
||||||
|
|
||||||
_attr_color_mode = ColorMode.BRIGHTNESS
|
_attr_color_mode = ColorMode.BRIGHTNESS
|
||||||
|
_attr_should_poll = False
|
||||||
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
|
||||||
_attr_supported_features = LightEntityFeature.TRANSITION
|
_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."""
|
"""Initialize a LiteJet light."""
|
||||||
self._config_entry = config_entry
|
self._config_entry = config_entry
|
||||||
self._lj = lj
|
self._lj = litejet
|
||||||
self._index = i
|
self._index = index
|
||||||
self._brightness = 0
|
self._attr_brightness = 0
|
||||||
self._name = name
|
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:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Run when this Entity has been added to HA."""
|
"""Run when this Entity has been added to HA."""
|
||||||
@ -63,41 +73,11 @@ class LiteJetLight(LightEntity):
|
|||||||
"""Entity being removed from hass."""
|
"""Entity being removed from hass."""
|
||||||
self._lj.unsubscribe(self._on_load_changed)
|
self._lj.unsubscribe(self._on_load_changed)
|
||||||
|
|
||||||
def _on_load_changed(self):
|
def _on_load_changed(self) -> None:
|
||||||
"""Handle state changes."""
|
"""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)
|
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:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn on the light."""
|
"""Turn on the light."""
|
||||||
|
|
||||||
@ -129,4 +109,5 @@ class LiteJetLight(LightEntity):
|
|||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Retrieve the light's brightness from the LiteJet system."""
|
"""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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user