From 84747ada668a2f07893e2b4093a586c4432b5e5f Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 3 Aug 2022 21:22:30 +0200 Subject: [PATCH] Use attributes in decora light (#76047) --- homeassistant/components/decora/light.py | 66 ++++++++---------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/decora/light.py b/homeassistant/components/decora/light.py index 9dbc031d476..c6fae73bc28 100644 --- a/homeassistant/components/decora/light.py +++ b/homeassistant/components/decora/light.py @@ -6,7 +6,7 @@ import copy from functools import wraps import logging import time -from typing import TypeVar +from typing import TYPE_CHECKING, Any, TypeVar from bluepy.btle import BTLEException # pylint: disable=import-error import decora # pylint: disable=import-error @@ -21,10 +21,13 @@ from homeassistant.components.light import ( LightEntity, ) from homeassistant.const import CONF_API_KEY, CONF_DEVICES, CONF_NAME -from homeassistant.core import HomeAssistant 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") _R = TypeVar("_R") @@ -110,65 +113,40 @@ class DecoraLight(LightEntity): _attr_color_mode = ColorMode.BRIGHTNESS _attr_supported_color_modes = {ColorMode.BRIGHTNESS} - def __init__(self, device): + def __init__(self, device: dict[str, Any]) -> None: """Initialize the light.""" - self._name = device["name"] - self._address = device["address"] + self._attr_name = device["name"] + self._attr_unique_id = device["address"] self._key = device["key"] - self._switch = decora.decora(self._address, self._key) - self._brightness = 0 - self._state = 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 + self._switch = decora.decora(device["address"], self._key) + self._attr_brightness = 0 + self._attr_is_on = False @retry - def set_state(self, brightness): + def set_state(self, brightness: int) -> None: """Set the state of this lamp to the provided brightness.""" self._switch.set_brightness(int(brightness / 2.55)) - self._brightness = brightness + self._attr_brightness = brightness @retry - def turn_on(self, **kwargs): + def turn_on(self, **kwargs: Any) -> None: """Turn the specified or all lights on.""" brightness = kwargs.get(ATTR_BRIGHTNESS) self._switch.on() - self._state = True + self._attr_is_on = True if brightness is not None: self.set_state(brightness) @retry - def turn_off(self, **kwargs): + def turn_off(self, **kwargs: Any) -> None: """Turn the specified or all lights off.""" self._switch.off() - self._state = False + self._attr_is_on = False @retry - def update(self): + def update(self) -> None: """Synchronise internal state with the actual light state.""" - self._brightness = self._switch.get_brightness() * 2.55 - self._state = self._switch.get_on() + self._attr_brightness = self._switch.get_brightness() * 2.55 + self._attr_is_on = self._switch.get_on()