Skip to content

Commit 4037103

Browse files
authored
Use shorthand attributes in tasmota lights (home-assistant#162290)
1 parent 9b3743a commit 4037103

File tree

1 file changed

+23
-58
lines changed
  • homeassistant/components/tasmota

1 file changed

+23
-58
lines changed

homeassistant/components/tasmota/light.py

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,8 @@ class TasmotaLight(
9595

9696
def __init__(self, **kwds: Any) -> None:
9797
"""Initialize Tasmota light."""
98-
self._supported_color_modes: set[ColorMode] | None = None
99-
100-
self._brightness: int | None = None
101-
self._color_mode: ColorMode | None = None
10298
self._color_temp: int | None = None
103-
self._effect: str | None = None
10499
self._white_value: int | None = None
105-
self._flash_times = None
106-
self._hs: tuple[float, float] | None = None
107100

108101
super().__init__(
109102
**kwds,
@@ -121,31 +114,31 @@ async def discovery_update(
121114

122115
def _setup_from_entity(self) -> None:
123116
"""(Re)Setup the entity."""
124-
self._supported_color_modes = set()
117+
self._attr_supported_color_modes = set()
125118
supported_features = LightEntityFeature(0)
126119
light_type = self._tasmota_entity.light_type
127120

128121
if light_type in [LIGHT_TYPE_RGB, LIGHT_TYPE_RGBW, LIGHT_TYPE_RGBCW]:
129122
# Mark HS support for RGBW light because we don't have direct
130123
# control over the white channel, so the base component's RGB->RGBW
131124
# translation does not work
132-
self._supported_color_modes.add(ColorMode.HS)
133-
self._color_mode = ColorMode.HS
125+
self._attr_supported_color_modes.add(ColorMode.HS)
126+
self._attr_color_mode = ColorMode.HS
134127

135128
if light_type == LIGHT_TYPE_RGBW:
136-
self._supported_color_modes.add(ColorMode.WHITE)
129+
self._attr_supported_color_modes.add(ColorMode.WHITE)
137130

138131
if light_type in [LIGHT_TYPE_COLDWARM, LIGHT_TYPE_RGBCW]:
139-
self._supported_color_modes.add(ColorMode.COLOR_TEMP)
140-
self._color_mode = ColorMode.COLOR_TEMP
132+
self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP)
133+
self._attr_color_mode = ColorMode.COLOR_TEMP
141134

142-
if light_type != LIGHT_TYPE_NONE and not self._supported_color_modes:
143-
self._supported_color_modes.add(ColorMode.BRIGHTNESS)
144-
self._color_mode = ColorMode.BRIGHTNESS
135+
if light_type != LIGHT_TYPE_NONE and not self._attr_supported_color_modes:
136+
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
137+
self._attr_color_mode = ColorMode.BRIGHTNESS
145138

146-
if not self._supported_color_modes:
147-
self._supported_color_modes.add(ColorMode.ONOFF)
148-
self._color_mode = ColorMode.ONOFF
139+
if not self._attr_supported_color_modes:
140+
self._attr_supported_color_modes.add(ColorMode.ONOFF)
141+
self._attr_color_mode = ColorMode.ONOFF
149142

150143
if light_type in [LIGHT_TYPE_RGB, LIGHT_TYPE_RGBW, LIGHT_TYPE_RGBCW]:
151144
supported_features |= LightEntityFeature.EFFECT
@@ -163,42 +156,32 @@ def state_updated(self, state: bool, **kwargs: Any) -> None:
163156
if "brightness" in attributes:
164157
brightness = float(attributes["brightness"])
165158
percent_bright = brightness / TASMOTA_BRIGHTNESS_MAX
166-
self._brightness = round(percent_bright * 255)
159+
self._attr_brightness = round(percent_bright * 255)
167160
if "color_hs" in attributes:
168-
self._hs = attributes["color_hs"]
161+
self._attr_hs_color = attributes["color_hs"]
169162
if "color_temp" in attributes:
170163
self._color_temp = attributes["color_temp"]
171164
if "effect" in attributes:
172-
self._effect = attributes["effect"]
165+
self._attr_effect = attributes["effect"]
173166
if "white_value" in attributes:
174167
white_value = float(attributes["white_value"])
175168
percent_white = white_value / TASMOTA_BRIGHTNESS_MAX
176169
self._white_value = round(percent_white * 255)
177170
if self._tasmota_entity.light_type == LIGHT_TYPE_RGBW:
178171
# Tasmota does not support RGBW mode, set mode to white or hs
179172
if self._white_value == 0:
180-
self._color_mode = ColorMode.HS
173+
self._attr_color_mode = ColorMode.HS
181174
else:
182-
self._color_mode = ColorMode.WHITE
175+
self._attr_color_mode = ColorMode.WHITE
183176
elif self._tasmota_entity.light_type == LIGHT_TYPE_RGBCW:
184177
# Tasmota does not support RGBWW mode, set mode to ct or hs
185178
if self._white_value == 0:
186-
self._color_mode = ColorMode.HS
179+
self._attr_color_mode = ColorMode.HS
187180
else:
188-
self._color_mode = ColorMode.COLOR_TEMP
181+
self._attr_color_mode = ColorMode.COLOR_TEMP
189182

190183
self.async_write_ha_state()
191184

192-
@property
193-
def brightness(self) -> int | None:
194-
"""Return the brightness of this light between 0..255."""
195-
return self._brightness
196-
197-
@property
198-
def color_mode(self) -> ColorMode | None:
199-
"""Return the color mode of the light."""
200-
return self._color_mode
201-
202185
@property
203186
def color_temp_kelvin(self) -> int | None:
204187
"""Return the color temperature value in Kelvin."""
@@ -222,40 +205,22 @@ def min_color_temp_kelvin(self) -> int:
222205
self._tasmota_entity.max_mireds
223206
)
224207

225-
@property
226-
def effect(self) -> str | None:
227-
"""Return the current effect."""
228-
return self._effect
229-
230208
@property
231209
def effect_list(self) -> list[str] | None:
232210
"""Return the list of supported effects."""
233211
return self._tasmota_entity.effect_list
234212

235-
@property
236-
def hs_color(self) -> tuple[float, float] | None:
237-
"""Return the hs color value."""
238-
if self._hs is None:
239-
return None
240-
hs_color = self._hs
241-
return (hs_color[0], hs_color[1])
242-
243-
@property
244-
def supported_color_modes(self) -> set[ColorMode] | None:
245-
"""Flag supported color modes."""
246-
return self._supported_color_modes
247-
248213
async def async_turn_on(self, **kwargs: Any) -> None:
249214
"""Turn the entity on."""
250-
supported_color_modes = self._supported_color_modes or set()
215+
supported_color_modes = self._attr_supported_color_modes
251216

252217
attributes: dict[str, Any] = {}
253218

254-
if ATTR_HS_COLOR in kwargs and ColorMode.HS in supported_color_modes:
219+
if ATTR_HS_COLOR in kwargs and ColorMode.HS in supported_color_modes: # type: ignore[operator]
255220
hs_color = kwargs[ATTR_HS_COLOR]
256221
attributes["color_hs"] = [hs_color[0], hs_color[1]]
257222

258-
if ATTR_WHITE in kwargs and ColorMode.WHITE in supported_color_modes:
223+
if ATTR_WHITE in kwargs and ColorMode.WHITE in supported_color_modes: # type: ignore[operator]
259224
attributes["white_value"] = scale_brightness(kwargs[ATTR_WHITE])
260225

261226
if ATTR_TRANSITION in kwargs:
@@ -266,7 +231,7 @@ async def async_turn_on(self, **kwargs: Any) -> None:
266231

267232
if (
268233
ATTR_COLOR_TEMP_KELVIN in kwargs
269-
and ColorMode.COLOR_TEMP in supported_color_modes
234+
and ColorMode.COLOR_TEMP in supported_color_modes # type: ignore[operator]
270235
):
271236
attributes["color_temp"] = color_util.color_temperature_kelvin_to_mired(
272237
kwargs[ATTR_COLOR_TEMP_KELVIN]

0 commit comments

Comments
 (0)