Use attributes in unifiled light (#76019)

This commit is contained in:
epenet 2022-08-04 23:10:27 +02:00 committed by GitHub
parent bb58ad0f54
commit a987cad973
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -63,58 +63,34 @@ class UnifiLedLight(LightEntity):
_attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS}
def __init__(self, light, api):
def __init__(self, light: dict[str, Any], api: unifiled) -> None:
"""Init Unifi LED Light."""
self._api = api
self._light = light
self._name = light["name"]
self._unique_id = light["id"]
self._state = light["status"]["output"]
self._available = light["isOnline"]
self._brightness = self._api.convertfrom100to255(light["status"]["led"])
@property
def name(self):
"""Return the display name of this light."""
return self._name
@property
def available(self):
"""Return the available state of this light."""
return self._available
@property
def brightness(self):
"""Return the brightness name of this light."""
return self._brightness
@property
def unique_id(self):
"""Return the unique id of this light."""
return self._unique_id
@property
def is_on(self):
"""Return true if light is on."""
return self._state
self._attr_name = light["name"]
self._light_id = light["id"]
self._attr_unique_id = light["id"]
self._attr_is_on = light["status"]["output"]
self._attr_available = light["isOnline"]
self._attr_brightness = self._api.convertfrom100to255(light["status"]["led"])
def turn_on(self, **kwargs: Any) -> None:
"""Instruct the light to turn on."""
self._api.setdevicebrightness(
self._unique_id,
self._light_id,
str(self._api.convertfrom255to100(kwargs.get(ATTR_BRIGHTNESS, 255))),
)
self._api.setdeviceoutput(self._unique_id, 1)
self._api.setdeviceoutput(self._light_id, 1)
def turn_off(self, **kwargs: Any) -> None:
"""Instruct the light to turn off."""
self._api.setdeviceoutput(self._unique_id, 0)
self._api.setdeviceoutput(self._light_id, 0)
def update(self) -> None:
"""Update the light states."""
self._state = self._api.getlightstate(self._unique_id)
self._brightness = self._api.convertfrom100to255(
self._api.getlightbrightness(self._unique_id)
self._attr_is_on = self._api.getlightstate(self._light_id)
self._attr_brightness = self._api.convertfrom100to255(
self._api.getlightbrightness(self._light_id)
)
self._available = self._api.getlightavailable(self._unique_id)
self._attr_available = self._api.getlightavailable(self._light_id)