Skip to content

Commit

Permalink
Use shorthand attributes in Kulersky (#99583)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek committed Sep 4, 2023
1 parent 8ea3b87 commit cf2d367
Showing 1 changed file with 14 additions and 27 deletions.
41 changes: 14 additions & 27 deletions homeassistant/components/kulersky/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ class KulerskyLight(LightEntity):

_attr_has_entity_name = True
_attr_name = None
_attr_available = False
_attr_supported_color_modes = {ColorMode.RGBW}
_attr_color_mode = ColorMode.RGBW

def __init__(self, light: pykulersky.Light) -> None:
"""Initialize a Kuler Sky light."""
self._light = light
self._available = False
self._attr_supported_color_modes = {ColorMode.RGBW}
self._attr_color_mode = ColorMode.RGBW
self._attr_unique_id = light.address
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, light.address)},
manufacturer="Brightech",
name=light.name,
)

async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
Expand All @@ -91,30 +97,11 @@ async def async_will_remove_from_hass(self, *args) -> None:
"Exception disconnected from %s", self._light.address, exc_info=True
)

@property
def unique_id(self):
"""Return the ID of this light."""
return self._light.address

@property
def device_info(self) -> DeviceInfo:
"""Device info for this light."""
return DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
manufacturer="Brightech",
name=self._light.name,
)

@property
def is_on(self):
"""Return true if light is on."""
return self.brightness > 0

@property
def available(self) -> bool:
"""Return True if entity is available."""
return self._available

async def async_turn_on(self, **kwargs: Any) -> None:
"""Instruct the light to turn on."""
default_rgbw = (255,) * 4 if self.rgbw_color is None else self.rgbw_color
Expand All @@ -140,18 +127,18 @@ async def async_turn_off(self, **kwargs: Any) -> None:
async def async_update(self) -> None:
"""Fetch new state data for this light."""
try:
if not self._available:
if not self._attr_available:
await self._light.connect()
rgbw = await self._light.get_color()
except pykulersky.PykulerskyException as exc:
if self._available:
if self._attr_available:
_LOGGER.warning("Unable to connect to %s: %s", self._light.address, exc)
self._available = False
self._attr_available = False
return
if self._available is False:
if self._attr_available is False:
_LOGGER.info("Reconnected to %s", self._light.address)

self._available = True
self._attr_available = True
brightness = max(rgbw)
if not brightness:
self._attr_rgbw_color = (0, 0, 0, 0)
Expand Down

0 comments on commit cf2d367

Please sign in to comment.