Use attributes in decora light (#76047)

This commit is contained in:
epenet 2022-08-03 21:22:30 +02:00 committed by GitHub
parent 34b0e0d062
commit 84747ada66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,7 +6,7 @@ import copy
from functools import wraps from functools import wraps
import logging import logging
import time import time
from typing import TypeVar from typing import TYPE_CHECKING, Any, TypeVar
from bluepy.btle import BTLEException # pylint: disable=import-error from bluepy.btle import BTLEException # pylint: disable=import-error
import decora # pylint: disable=import-error import decora # pylint: disable=import-error
@ -21,10 +21,13 @@ from homeassistant.components.light import (
LightEntity, LightEntity,
) )
from homeassistant.const import CONF_API_KEY, CONF_DEVICES, CONF_NAME from homeassistant.const import CONF_API_KEY, CONF_DEVICES, CONF_NAME
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType if TYPE_CHECKING:
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
_DecoraLightT = TypeVar("_DecoraLightT", bound="DecoraLight") _DecoraLightT = TypeVar("_DecoraLightT", bound="DecoraLight")
_R = TypeVar("_R") _R = TypeVar("_R")
@ -110,65 +113,40 @@ class DecoraLight(LightEntity):
_attr_color_mode = ColorMode.BRIGHTNESS _attr_color_mode = ColorMode.BRIGHTNESS
_attr_supported_color_modes = {ColorMode.BRIGHTNESS} _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
def __init__(self, device): def __init__(self, device: dict[str, Any]) -> None:
"""Initialize the light.""" """Initialize the light."""
self._name = device["name"] self._attr_name = device["name"]
self._address = device["address"] self._attr_unique_id = device["address"]
self._key = device["key"] self._key = device["key"]
self._switch = decora.decora(self._address, self._key) self._switch = decora.decora(device["address"], self._key)
self._brightness = 0 self._attr_brightness = 0
self._state = False self._attr_is_on = False
@property
def unique_id(self):
"""Return the ID of this light."""
return self._address
@property
def name(self):
"""Return the name of the device if any."""
return self._name
@property
def is_on(self):
"""Return true if device is on."""
return self._state
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return self._brightness
@property
def assumed_state(self):
"""We can read the actual state."""
return False
@retry @retry
def set_state(self, brightness): def set_state(self, brightness: int) -> None:
"""Set the state of this lamp to the provided brightness.""" """Set the state of this lamp to the provided brightness."""
self._switch.set_brightness(int(brightness / 2.55)) self._switch.set_brightness(int(brightness / 2.55))
self._brightness = brightness self._attr_brightness = brightness
@retry @retry
def turn_on(self, **kwargs): def turn_on(self, **kwargs: Any) -> None:
"""Turn the specified or all lights on.""" """Turn the specified or all lights on."""
brightness = kwargs.get(ATTR_BRIGHTNESS) brightness = kwargs.get(ATTR_BRIGHTNESS)
self._switch.on() self._switch.on()
self._state = True self._attr_is_on = True
if brightness is not None: if brightness is not None:
self.set_state(brightness) self.set_state(brightness)
@retry @retry
def turn_off(self, **kwargs): def turn_off(self, **kwargs: Any) -> None:
"""Turn the specified or all lights off.""" """Turn the specified or all lights off."""
self._switch.off() self._switch.off()
self._state = False self._attr_is_on = False
@retry @retry
def update(self): def update(self) -> None:
"""Synchronise internal state with the actual light state.""" """Synchronise internal state with the actual light state."""
self._brightness = self._switch.get_brightness() * 2.55 self._attr_brightness = self._switch.get_brightness() * 2.55
self._state = self._switch.get_on() self._attr_is_on = self._switch.get_on()