Use entity attributes in mystrom (#93591)

* Migrated proprties to _attr_

* Update homeassistant/components/mystrom/light.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/light.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/light.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/light.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/light.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/light.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/switch.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* Update homeassistant/components/mystrom/switch.py

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

* review comment

---------

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
pull/93608/head
Paul Frank 2023-05-26 16:22:59 +02:00 committed by GitHub
parent 5671934b34
commit 0bf9bb15f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 78 deletions

View File

@ -31,8 +31,6 @@ DEFAULT_NAME = "myStrom bulb"
EFFECT_RAINBOW = "rainbow"
EFFECT_SUNRISE = "sunrise"
MYSTROM_EFFECT_LIST = [EFFECT_RAINBOW, EFFECT_SUNRISE]
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
@ -74,52 +72,15 @@ class MyStromLight(LightEntity):
_attr_color_mode = ColorMode.HS
_attr_supported_color_modes = {ColorMode.HS}
_attr_supported_features = LightEntityFeature.EFFECT | LightEntityFeature.FLASH
_attr_effect_list = [EFFECT_RAINBOW, EFFECT_SUNRISE]
def __init__(self, bulb, name, mac):
"""Initialize the light."""
self._bulb = bulb
self._name = name
self._state = None
self._available = False
self._brightness = 0
self._color_h = 0
self._color_s = 0
self._mac = mac
@property
def name(self):
"""Return the display name of this light."""
return self._name
@property
def unique_id(self):
"""Return a unique ID."""
return self._mac
@property
def brightness(self):
"""Return the brightness of the light."""
return self._brightness
@property
def hs_color(self):
"""Return the color of the light."""
return self._color_h, self._color_s
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available
@property
def effect_list(self):
"""Return the list of supported effects."""
return MYSTROM_EFFECT_LIST
@property
def is_on(self):
"""Return true if light is on."""
return self._state
self._attr_name = name
self._attr_available = False
self._attr_unique_id = mac
self._attr_hs_color = 0, 0
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the light."""
@ -130,7 +91,10 @@ class MyStromLight(LightEntity):
color_h, color_s = kwargs[ATTR_HS_COLOR]
elif ATTR_BRIGHTNESS in kwargs:
# Brightness update, keep color
color_h, color_s = self._color_h, self._color_s
if self.hs_color is not None:
color_h, color_s = self.hs_color
else:
color_h, color_s = 0, 0 # Back to white
else:
color_h, color_s = 0, 0 # Back to white
@ -159,7 +123,7 @@ class MyStromLight(LightEntity):
"""Fetch new state data for this light."""
try:
await self._bulb.get_state()
self._state = self._bulb.state
self._attr_is_on = self._bulb.state
colors = self._bulb.color
try:
@ -168,11 +132,10 @@ class MyStromLight(LightEntity):
color_s, color_v = colors.split(";")
color_h = 0
self._color_h = int(color_h)
self._color_s = int(color_s)
self._brightness = int(color_v) * 255 / 100
self._attr_hs_color = int(color_h), int(color_s)
self._attr_brightness = int(int(color_v) * 255 / 100)
self._available = True
self._attr_available = True
except MyStromConnectionError:
_LOGGER.warning("No route to myStrom bulb")
self._available = False
self._attr_available = False

View File

@ -53,30 +53,9 @@ class MyStromSwitch(SwitchEntity):
def __init__(self, plug, name):
"""Initialize the myStrom switch/plug."""
self._name = name
self.plug = plug
self._available = True
self.relay = None
@property
def name(self):
"""Return the name of the switch."""
return self._name
@property
def is_on(self):
"""Return true if switch is on."""
return bool(self.relay)
@property
def unique_id(self):
"""Return a unique ID."""
return self.plug._mac # pylint: disable=protected-access
@property
def available(self):
"""Could the device be accessed during the last update call."""
return self._available
self._attr_name = name
self._attr_unique_id = self.plug.mac
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the switch on."""
@ -96,9 +75,9 @@ class MyStromSwitch(SwitchEntity):
"""Get the latest data from the device and update the data."""
try:
await self.plug.get_state()
self.relay = self.plug.relay
self._available = True
self._attr_is_on = self.plug.relay
self._attr_available = True
except MyStromConnectionError:
if self._available:
self._available = False
if self.available:
self._attr_available = False
_LOGGER.error("No route to myStrom plug")