add support for dimming/brightening X10 lamps (#130196)

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Kerey Roper 2025-01-09 04:07:24 -08:00 committed by GitHub
parent 411d14c2ce
commit 6a4160bcc4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -81,9 +81,16 @@ class X10Light(LightEntity):
@property
def brightness(self):
"""Return the brightness of the light."""
"""Return the brightness of the light, scaled to base class 0..255.
This needs to be scaled from 0..x for use with X10 dimmers.
"""
return self._brightness
def normalize_x10_brightness(self, brightness: float) -> float:
"""Return calculated brightness values."""
return int((brightness / 255) * 32)
@property
def is_on(self):
"""Return true if light is on."""
@ -91,11 +98,37 @@ class X10Light(LightEntity):
def turn_on(self, **kwargs: Any) -> None:
"""Instruct the light to turn on."""
if self._is_cm11a:
x10_command(f"on {self._id}")
else:
x10_command(f"fon {self._id}")
old_brightness = self._brightness
if old_brightness == 0:
# Dim down from max if applicable, also avoids a "dim" command if an "on" is more appropriate
old_brightness = 255
self._brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
brightness_diff = self.normalize_x10_brightness(
self._brightness
) - self.normalize_x10_brightness(old_brightness)
command_suffix = ""
# heyu has quite a messy command structure - we'll just deal with it here
if brightness_diff == 0:
if self._is_cm11a:
command_prefix = "on"
else:
command_prefix = "fon"
elif brightness_diff > 0:
if self._is_cm11a:
command_prefix = "bright"
else:
command_prefix = "fbright"
command_suffix = f" {brightness_diff}"
else:
if self._is_cm11a:
if self._state:
command_prefix = "dim"
else:
command_prefix = "dimb"
else:
command_prefix = "fdim"
command_suffix = f" {-brightness_diff}"
x10_command(f"{command_prefix} {self._id}{command_suffix}")
self._state = True
def turn_off(self, **kwargs: Any) -> None:
@ -104,6 +137,7 @@ class X10Light(LightEntity):
x10_command(f"off {self._id}")
else:
x10_command(f"foff {self._id}")
self._brightness = 0
self._state = False
def update(self) -> None: