From 31530a5abcca44a8d45ab018b6e092e70473f741 Mon Sep 17 00:00:00 2001 From: Xavi Date: Sun, 24 Nov 2019 21:26:14 +0100 Subject: [PATCH] feat(group of sensors and lights): Adding the possibility to add a group of lights and/or sensors on the configuration --- README.md | 30 +++++++++---------- .../z2m_ikea_controller.py | 25 ++++++++++++++-- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 1c4c33ef..2c62229d 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,13 @@ _Bring full functionality to IKEA light controllers_ This automation will bring the following functionalities to IKEA E1524/E1810: -- Toggle light +- Toggle light(s) - Manual increase/decrease of brightness and color temperature - Smooth increase/decrease (holding button) of brightness and color temperature This automation will bring the following functionalities to IKEA E1743: -- Turn on/Turn off light +- Turn on/Turn off light(s) - Manual increase/decrease of brightness - Smooth increase/decrease (holding button) of brightness @@ -34,7 +34,7 @@ For IKEA E1524/E1810: nameOfYourInstanceApp: module: z2m_ikea_controller class: E1810Controller - sensor: + sensor: light: ``` @@ -44,18 +44,18 @@ For IKEA E1743: nameOfYourInstanceApp: module: z2m_ikea_controller class: E1743Controller - sensor: + sensor: light: ``` -_Note: This was tested with both devices and Zigbee2MQTT, but the code does not use any MQTT calls, just Home assistant API._ - -| key | optional | type | default | example | description | -| ----------------- | -------- | ------ | ------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `module` | False | string | - | `z2m_ikea_controller` | The Python module | -| `class` | False | string | - | `E1810Controller` | The Python class | -| `sensor` | False | string | - | `sensor.livingroom_controller_action` | The sensor entity id from HA. Note that for IKEA E1524/E1810 it finishes with "\_action" by default and for IKEA E1743 with "\_click". | -| `light` | False | string | - | `light.livingroom` | The light you want to control | -| `manual_steps` | True | int | 10 | | Number of steps to go from min to max when clicking. If the value is 2 with one click you will set the light to 50% and with another one to 100%. | -| `automatic_steps` | True | int | 20 | | Number of steps to go from min to max when smoothing. If the value is 2 with one click you will set the light to 50% and with another one to 100%. | -| `delay` | True | int | 150 | | Delay in milliseconds that takes between sending the instructions to the light (for the smooth functionality). Note that the maximum value is 1000 and if leaving to 0, you might get uncommon behaviour. | +_Note: This was tested with both devices, Zigbee2MQTT and IKEA lights, but the code does not use any MQTT calls, just Home assistant API._ + +| key | optional | type | default | example | description | +| ----------------- | -------- | -------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `module` | False | string | - | `z2m_ikea_controller` | The Python module | +| `class` | False | string | - | `E1810Controller` | The Python class | +| `sensor` | False | string \| list | - | `sensor.livingroom_controller_action` or `sensor.livingroom_sensor.livingroom_controller_action1, sensor.livingroom_controller_action2` | The sensor(s) entity id from HA. Note that for IKEA E1524/E1810 it finishes with "\_action" by default and for IKEA E1743 with "\_click". This can be also sent as list on the YAML (using "-") | +| `light` | False | string | - | `group.livingroom_lights` or `light.kitchen` | The light (or group of lights) you want to control | +| `manual_steps` | True | int | 10 | | Number of steps to go from min to max when clicking. If the value is 2 with one click you will set the light to 50% and with another one to 100%. | +| `automatic_steps` | True | int | 20 | | Number of steps to go from min to max when smoothing. If the value is 2 with one click you will set the light to 50% and with another one to 100%. | +| `delay` | True | int | 150 | | Delay in milliseconds that takes between sending the instructions to the light (for the smooth functionality). Note that the maximum value is 1000 and if leaving to 0, you might get uncommon behaviour. | diff --git a/apps/z2m_ikea_controller/z2m_ikea_controller.py b/apps/z2m_ikea_controller/z2m_ikea_controller.py index 46e58df8..e5839343 100755 --- a/apps/z2m_ikea_controller/z2m_ikea_controller.py +++ b/apps/z2m_ikea_controller/z2m_ikea_controller.py @@ -20,14 +20,22 @@ class IkeaController(hass.Hass): def initialize(self): - self.sensor = self.args["sensor"] + self.sensors = self.get_sensors(self.args["sensor"]) self.light = self.args["light"] # Since time.sleep is not recommended I limited to 1s self.delay = min(1000, self.args.get("delay", DEFAULT_DELAY)) self.manual_steps = self.args.get("manual_steps", DEFAULT_MANUAL_STEPS) self.automatic_steps = self.args.get("automatic_steps", DEFAULT_AUTOMATIC_STEPS) self.on_hold = False - self.listen_state(self.state, self.sensor) + for sensor in self.sensors: + self.listen_state(self.state, sensor) + + def get_sensors(self, sensors): + type_ = type(sensors) + if type_ == str: + return sensors.replace(" ", "").split(",") + elif type_ == list: + return sensors def process_state(self, state): """ @@ -42,6 +50,7 @@ def state(self, entity, attribute, old, new, kwargs): if new == "": return attribute, direction, action = self.process_state(new) + light_state = self.get_state(self.light) if action == "toggle": self.toggle(self.light) elif action == "on": @@ -51,8 +60,10 @@ def state(self, entity, attribute, old, new, kwargs): elif action == "release": self.on_hold = False else: + if light_state == "off": + return sign = sign_mapping[direction] - value = self.get_state(self.light, attribute=attribute) + value = self.get_attr_value(self.light, attribute) max_ = attribute_minmax[attribute]["max"] min_ = attribute_minmax[attribute]["min"] if action == "click": @@ -68,6 +79,13 @@ def state(self, entity, attribute, old, new, kwargs): # https://github.com/home-assistant/appdaemon/issues/26#issuecomment-274798324 time.sleep(self.delay / 1000) + def get_attr_value(self, light, attribute): + if "group." in light: + lights = self.get_state(self.light, attribute="entity_id") + light = lights[0] + out = self.get_state(light, attribute=attribute) + return out + def turn_on_light(self, attribute, old, sign, steps): """ It returns the new value if it didn't reached min or max. Otherwise None. @@ -84,6 +102,7 @@ def turn_on_light(self, attribute, old, sign, steps): self.turn_on(self.light, **{attribute: new_state_attribute}) return None + class E1810Controller(IkeaController): # Different states reported from the controller: # toggle, brightness_up_click, brightness_down_click