Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for "setRGB" command for light entity. #75

Merged
merged 18 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion custom_components/tahoma/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,18 @@
CORE_OCCUPANCY_STATE = "core:OccupancyState"
CORE_SMOKE_STATE = "core:SmokeState"
CORE_TEMPERATURE_STATE = "core:TemperatureState"
CORE_LUMINANCE_STATE = "core:LuminanceState"
CORE_RELATIVE_HUMIDITY_STATE = "core:RelativeHumidityState"
CORE_MEMORIZED_1_POSITION_STATE = "core:Memorized1PositionState"
CORE_PEDESTRIAN_POSITION_STATE = "core:PedestrianPositionState"
CORE_ELECTRIC_POWER_CONSUMPTION_STATE = "core:ElectricPowerConsumptionState"
CORE_CO2_CONCENTRATION_STATE = "core:CO2ConcentrationState"

# Light core states
CORE_LUMINANCE_STATE = "core:LuminanceState"
CORE_RED_COLOR_INTENSITY_STATE = "core:RedColorIntensityState"
CORE_GREEN_COLOR_INTENSITY_STATE = "core:GreenColorIntensityState"
CORE_BLUE_COLOR_INTENSITY_STATE = "core:BlueColorIntensityState"

IO_PRIORITY_LOCK_LEVEL_STATE = "io:PriorityLockLevelState"
IO_PRIORITY_LOCK_ORIGINATOR_STATE = "io:PriorityLockOriginatorState"

Expand Down
54 changes: 45 additions & 9 deletions custom_components/tahoma/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,22 @@
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_EFFECT,
ATTR_HS_COLOR,
SUPPORT_BRIGHTNESS,
SUPPORT_COLOR,
SUPPORT_EFFECT,
LightEntity,
)
from homeassistant.const import STATE_OFF, STATE_ON

from .const import DOMAIN, TAHOMA_TYPES
import homeassistant.util.color as color_util

from .const import (
CORE_BLUE_COLOR_INTENSITY_STATE,
CORE_GREEN_COLOR_INTENSITY_STATE,
CORE_RED_COLOR_INTENSITY_STATE,
DOMAIN,
TAHOMA_TYPES,
)
from .tahoma_device import TahomaDevice

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -41,10 +50,10 @@ def __init__(self, tahoma_device, controller):
"""Initialize a device."""
super().__init__(tahoma_device, controller)

self._skip_update = False
self._effect = None
self._brightness = None
self._state = None
self._hs_color = []

@property
def brightness(self) -> int:
Expand All @@ -56,6 +65,13 @@ def is_on(self) -> bool:
"""Return true if light is on."""
return self._state

@property
def hs_color(self):
"""Return the hue and saturation color value [float, float]."""
if self._hs_color:
return self._hs_color
return None

@property
def supported_features(self) -> int:
"""Flag supported features."""
Expand All @@ -68,13 +84,23 @@ def supported_features(self) -> int:
if "wink" in self.tahoma_device.command_definitions:
supported_features |= SUPPORT_EFFECT

if "setRGB" in self.tahoma_device.command_definitions:
supported_features |= SUPPORT_COLOR

return supported_features

def turn_on(self, **kwargs) -> None:
"""Turn the light on."""
self._state = True
self._skip_update = True

if ATTR_HS_COLOR in kwargs:
self.apply_action(
"setRGB",
*[
int(float(c))
for c in color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
],
)
if ATTR_BRIGHTNESS in kwargs:
self._brightness = int(float(kwargs[ATTR_BRIGHTNESS]) / 255 * 100)
self.apply_action("setIntensity", self._brightness)
Expand All @@ -89,7 +115,6 @@ def turn_on(self, **kwargs) -> None:
def turn_off(self, **kwargs) -> None:
"""Turn the light off."""
self._state = False
self._skip_update = True
self.apply_action("off")

self.async_write_ha_state()
Expand All @@ -109,10 +134,6 @@ def update(self):

This is the only method that should fetch new data for Home Assistant.
"""
# Postpone the immediate state check for changes that take time.
if self._skip_update:
self._skip_update = False
return

self.controller.get_states([self.tahoma_device])

Expand All @@ -125,3 +146,18 @@ def update(self):
self._state = True
else:
self._state = False

if CORE_RED_COLOR_INTENSITY_STATE in self.tahoma_device.active_states:
self._hs_color = color_util.color_RGB_to_hs(
[
self.tahoma_device.active_states.get(
CORE_RED_COLOR_INTENSITY_STATE
),
self.tahoma_device.active_states.get(
CORE_GREEN_COLOR_INTENSITY_STATE
),
self.tahoma_device.active_states.get(
CORE_BLUE_COLOR_INTENSITY_STATE
),
]
)