Skip to content

Commit

Permalink
feat(group of sensors and lights):
Browse files Browse the repository at this point in the history
Adding the possibility to add a group of lights and/or sensors on the configuration
  • Loading branch information
xaviml committed Nov 24, 2019
1 parent b110399 commit 31530a5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -34,7 +34,7 @@ For IKEA E1524/E1810:
nameOfYourInstanceApp:
module: z2m_ikea_controller
class: E1810Controller
sensor: <sensor entity id>
sensor: <sensor(s) entity id>
light: <light or group entity id>
```
Expand All @@ -44,18 +44,18 @@ For IKEA E1743:
nameOfYourInstanceApp:
module: z2m_ikea_controller
class: E1743Controller
sensor: <sensor entity id>
sensor: <sensor(s) entity id>
light: <light or group entity id>
```
_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. |
25 changes: 22 additions & 3 deletions apps/z2m_ikea_controller/z2m_ikea_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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":
Expand All @@ -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":
Expand All @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 31530a5

Please sign in to comment.