Skip to content

Commit

Permalink
add support for dimming/brightening X10 lamps (home-assistant#130196)
Browse files Browse the repository at this point in the history
Co-authored-by: Joost Lekkerkerker <[email protected]>
  • Loading branch information
kereyroper and joostlek authored Jan 9, 2025
1 parent 411d14c commit 6a4160b
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions homeassistant/components/x10/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,54 @@ def name(self):

@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."""
return self._state

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:
Expand All @@ -104,6 +137,7 @@ def turn_off(self, **kwargs: Any) -> None:
x10_command(f"off {self._id}")
else:
x10_command(f"foff {self._id}")
self._brightness = 0
self._state = False

def update(self) -> None:
Expand Down

0 comments on commit 6a4160b

Please sign in to comment.