Use attributes in zengge light (#75994)

This commit is contained in:
epenet 2022-08-04 01:15:56 +02:00 committed by GitHub
parent 847f150a78
commit 1ff7686160
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -53,49 +53,28 @@ def setup_platform(
class ZenggeLight(LightEntity): class ZenggeLight(LightEntity):
"""Representation of a Zengge light.""" """Representation of a Zengge light."""
_attr_supported_color_modes = {ColorMode.HS, ColorMode.WHITE}
def __init__(self, device): def __init__(self, device):
"""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.is_valid = True self.is_valid = True
self._bulb = zengge(self._address) self._bulb = zengge(device["address"])
self._white = 0 self._white = 0
self._brightness = 0 self._attr_brightness = 0
self._hs_color = (0, 0) self._attr_hs_color = (0, 0)
self._state = False self._attr_is_on = False
if self._bulb.connect() is False: if self._bulb.connect() is False:
self.is_valid = False self.is_valid = False
_LOGGER.error("Failed to connect to bulb %s, %s", self._address, self._name) _LOGGER.error(
"Failed to connect to bulb %s, %s", device["address"], device["name"]
)
return return
@property @property
def unique_id(self): def white_value(self) -> int:
"""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 property."""
return self._brightness
@property
def hs_color(self):
"""Return the color property."""
return self._hs_color
@property
def white_value(self):
"""Return the white property.""" """Return the white property."""
return self._white return self._white
@ -106,19 +85,9 @@ class ZenggeLight(LightEntity):
return ColorMode.WHITE return ColorMode.WHITE
return ColorMode.HS return ColorMode.HS
@property def _set_rgb(self, red: int, green: int, blue: int) -> None:
def supported_color_modes(self) -> set[ColorMode | str]:
"""Flag supported color modes."""
return {ColorMode.HS, ColorMode.WHITE}
@property
def assumed_state(self):
"""We can report the actual state."""
return False
def _set_rgb(self, red, green, blue):
"""Set the rgb state.""" """Set the rgb state."""
return self._bulb.set_rgb(red, green, blue) self._bulb.set_rgb(red, green, blue)
def _set_white(self, white): def _set_white(self, white):
"""Set the white state.""" """Set the white state."""
@ -126,7 +95,7 @@ class ZenggeLight(LightEntity):
def turn_on(self, **kwargs: Any) -> None: def turn_on(self, **kwargs: Any) -> None:
"""Turn the specified light on.""" """Turn the specified light on."""
self._state = True self._attr_is_on = True
self._bulb.on() self._bulb.on()
hs_color = kwargs.get(ATTR_HS_COLOR) hs_color = kwargs.get(ATTR_HS_COLOR)
@ -135,37 +104,40 @@ class ZenggeLight(LightEntity):
if white is not None: if white is not None:
# Change the bulb to white # Change the bulb to white
self._brightness = self._white = white self._attr_brightness = white
self._hs_color = (0, 0) self._white = white
self._attr_hs_color = (0, 0)
if hs_color is not None: if hs_color is not None:
# Change the bulb to hs # Change the bulb to hs
self._white = 0 self._white = 0
self._hs_color = hs_color self._attr_hs_color = hs_color
if brightness is not None: if brightness is not None:
self._brightness = brightness self._attr_brightness = brightness
if self._white != 0: if self._white != 0:
self._set_white(self._brightness) self._set_white(self.brightness)
else: else:
assert self.hs_color is not None
assert self.brightness is not None
rgb = color_util.color_hsv_to_RGB( rgb = color_util.color_hsv_to_RGB(
self._hs_color[0], self._hs_color[1], self._brightness / 255 * 100 self.hs_color[0], self.hs_color[1], self.brightness / 255 * 100
) )
self._set_rgb(*rgb) self._set_rgb(*rgb)
def turn_off(self, **kwargs: Any) -> None: def turn_off(self, **kwargs: Any) -> None:
"""Turn the specified light off.""" """Turn the specified light off."""
self._state = False self._attr_is_on = False
self._bulb.off() self._bulb.off()
def update(self) -> None: def update(self) -> None:
"""Synchronise internal state with the actual light state.""" """Synchronise internal state with the actual light state."""
rgb = self._bulb.get_colour() rgb = self._bulb.get_colour()
hsv = color_util.color_RGB_to_hsv(*rgb) hsv = color_util.color_RGB_to_hsv(*rgb)
self._hs_color = hsv[:2] self._attr_hs_color = hsv[:2]
self._brightness = (hsv[2] / 100) * 255 self._attr_brightness = int((hsv[2] / 100) * 255)
self._white = self._bulb.get_white() self._white = self._bulb.get_white()
if self._white: if self._white:
self._brightness = self._white self._attr_brightness = self._white
self._state = self._bulb.get_on() self._attr_is_on = self._bulb.get_on()