Skip to content

Commit

Permalink
Reworked light platform.
Browse files Browse the repository at this point in the history
Fix #217 deprecation warnings and light bugfixes.
Bumped to boschshcpy==0.2.96
  • Loading branch information
tschamm committed Jan 6, 2025
1 parent bf9d515 commit a63a92b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
78 changes: 39 additions & 39 deletions custom_components/bosch_shc/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
LightEntity,
)
from homeassistant.const import Platform
from homeassistant.util.color import (
color_hs_to_RGB,
color_RGB_to_hs,
color_temperature_mired_to_kelvin,
color_temperature_to_hs,
)
from homeassistant.util import color as color_util

from .const import DATA_SESSION, DOMAIN
from .entity import SHCEntity, async_migrate_to_new_unique_id
Expand Down Expand Up @@ -44,22 +39,39 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class LightSwitch(SHCEntity, LightEntity):
"""Representation of a SHC controlled light."""

_attr_supported_color_modes: set[ColorMode] | set[str] | None = None

def __init__(self, device, entry_id) -> None:
super().__init__(device=device, entry_id=entry_id)
self._attr_supported_color_modes: set[ColorMode | str] = set()
self._attr_color_mode = ColorMode.BRIGHTNESS
self._attr_supported_color_modes: set[ColorMode] = set()

if self._device.supports_color_hsb:
self._attr_supported_color_modes.add(ColorMode.HS)
self._attr_color_mode = ColorMode.HS
if self._device.supports_color_temp:
self._attr_supported_color_modes.add(ColorMode.COLOR_TEMP)
self._attr_color_mode = ColorMode.COLOR_TEMP
if self._device.supports_color_hsb or self._device.supports_color_temp:
self._attr_min_color_temp_kelvin = (
color_util.color_temperature_mired_to_kelvin(
self._device.min_color_temperature
)
)
self._attr_max_color_temp_kelvin = (
color_util.color_temperature_mired_to_kelvin(
self._device.max_color_temperature
)
)
if self._device.supports_brightness:
if len(self._attr_supported_color_modes) == 0:
# only add color mode brightness if no color variants
if (
len(self._attr_supported_color_modes) == 0
): # BRIGHTNESS must be the only supported mode
self._attr_supported_color_modes.add(ColorMode.BRIGHTNESS)
self._attr_color_mode = ColorMode.BRIGHTNESS
else:
if (
len(self._attr_supported_color_modes) == 0
): # ONOFF must be the only supported mode
self._attr_supported_color_modes.add(ColorMode.ONOFF)
self._attr_color_mode = ColorMode.ONOFF

@property
def is_on(self):
Expand All @@ -69,50 +81,38 @@ def is_on(self):
@property
def brightness(self) -> int:
"""Return the brightness of this light between 0..255."""
brightness_value = (
round(self._device.brightness * 255 / 100)
if self._device.brightness
else None
)
return brightness_value
return round(self._device.brightness * 255 / 100)

@property
def hs_color(self):
"""Return the rgb color of this light."""
rgb_raw = self._device.rgb
rgb = ((rgb_raw >> 16) & 0xFF, (rgb_raw >> 8) & 0xFF, rgb_raw & 0xFF)
return color_RGB_to_hs(*rgb)
return color_util.color_RGB_to_hs(*rgb)

@property
def color_temp(self):
def color_temp_kelvin(self):
"""Return the color temp of this light."""
if self._device.supports_color_temp:
return self._device.color
return None
return color_util.color_temperature_mired_to_kelvin(self._device.color)

def turn_on(self, **kwargs):
"""Turn the light on."""
hs_color = kwargs.get(ATTR_HS_COLOR)
color_temp = kwargs.get(ATTR_COLOR_TEMP_KELVIN)
color_temp_kelvin = kwargs.get(ATTR_COLOR_TEMP_KELVIN)
brightness = kwargs.get(ATTR_BRIGHTNESS)

if brightness is not None and self._device.supports_brightness:
self._device.brightness = round(brightness * 100 / 255)
if self._device.supports_color_hsb:
if color_temp is not None:
if color_temp < self._device.min_color_temperature:
color_temp = self._device.min_color_temperature
if color_temp > self._device.max_color_temperature:
color_temp = self._device.max_color_temperature
hs_color = color_temperature_to_hs(
color_temperature_mired_to_kelvin(color_temp)
)
if hs_color is not None:
rgb = color_hs_to_RGB(*hs_color)
raw_rgb = (rgb[0] << 16) + (rgb[1] << 8) + rgb[2]
self._device.rgb = raw_rgb
if color_temp is not None and self._device.supports_color_temp:
self._device.color = color_temp

if color_temp_kelvin is not None and self._device.supports_color_temp:
self._device.color = color_util.color_temperature_kelvin_to_mired(
color_temp_kelvin
)

if hs_color is not None and self._device.supports_color_hsb:
rgb = color_util.color_hs_to_RGB(*hs_color)
raw_rgb = (rgb[0] << 16) + (rgb[1] << 8) + rgb[2]
self._device.rgb = raw_rgb

if not self.is_on:
self._device.binarystate = True
Expand Down
4 changes: 2 additions & 2 deletions custom_components/bosch_shc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"documentation": "https://github.com/tschamm/boschshc-hass/blob/master/README.md",
"iot_class": "local_push",
"issue_tracker": "https://github.com/tschamm/boschshc-hass/issues",
"requirements": ["boschshcpy==0.2.95"],
"version": "0.4.93",
"requirements": ["boschshcpy==0.2.96"],
"version": "0.4.94",
"zeroconf": [{ "type": "_http._tcp.local.", "name": "bosch shc*" }]
}

0 comments on commit a63a92b

Please sign in to comment.