Skip to content

Commit

Permalink
Merge pull request #156 from oliverblaha/master
Browse files Browse the repository at this point in the history
Moved DEFAULT_* values out of public Home Assistant methods
  • Loading branch information
Julius2342 authored Dec 22, 2018
2 parents f6aea6a + 6d30ba0 commit 6d5a13c
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions home-assistant-plugin/custom_components/light/xknx.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def should_poll(self):
def brightness(self):
"""Return the brightness of this light between 0..255."""
if self.device.supports_color:
return max(self.device.current_color) \
if self.device.current_color is not None else \
DEFAULT_BRIGHTNESS
if self.device.current_color is None:
return None
return max(self.device.current_color)
if self.device.supports_brightness:
return self.device.current_brightness
return None
Expand All @@ -124,8 +124,9 @@ def hs_color(self):
"""Return the HS color value."""
if self.device.supports_color:
rgb = self.device.current_color
return color_util.color_RGB_to_hs(*rgb) \
if rgb is not None else DEFAULT_COLOR
if rgb is None:
return None
return color_util.color_RGB_to_hs(*rgb)
return None

@property
Expand Down Expand Up @@ -165,30 +166,41 @@ def supported_features(self):

async def async_turn_on(self, **kwargs):
"""Turn the light on."""
brightness = int(kwargs[ATTR_BRIGHTNESS]) \
if ATTR_BRIGHTNESS in kwargs else \
self.brightness
hs_color = kwargs[ATTR_HS_COLOR] \
if ATTR_HS_COLOR in kwargs else \
self.hs_color

update_color = ATTR_HS_COLOR in kwargs
# initialize with current values
brightness = self.brightness
hs_color = self.hs_color

# overwrite values with new settings, if available
if ATTR_BRIGHTNESS in kwargs:
brightness = int(kwargs[ATTR_BRIGHTNESS])
if ATTR_HS_COLOR in kwargs:
hs_color = kwargs[ATTR_HS_COLOR]

# fall back to default values, if required
if brightness is None:
brightness = DEFAULT_BRIGHTNESS
if hs_color is None:
hs_color = DEFAULT_COLOR

update_brightness = ATTR_BRIGHTNESS in kwargs
update_color = ATTR_HS_COLOR in kwargs

# always only go one path for turning on (avoid conflicting changes
# and weird effects)
if self.device.supports_brightness and \
(update_brightness and not update_color):
# if we don't need to update the color, try updating brightness
# directly if supported
# directly if supported; don't do it if color also has to be changed,
# as RGB color implicitly sets the brightness as well
await self.device.set_brightness(brightness)
elif self.device.supports_color and \
(update_brightness or update_color):
# change RGB color (includes brightness)
await self.device.set_color(
color_util.color_hsv_to_RGB(*hs_color, brightness * 100 / 255))
else:
# no color/brightness change, so just turn it on
# no color/brightness change requested, so just turn it on
await self.device.set_on()

async def async_turn_off(self, **kwargs):
Expand Down

0 comments on commit 6d5a13c

Please sign in to comment.