Skip to content

Commit 13b191a

Browse files
committed
feat(z2m): add event entity listener
related to #1090
1 parent 01fbb86 commit 13b191a

33 files changed

+657
-278
lines changed

RELEASE_NOTES.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ _This minor change does not contain any breaking changes._
66

77
**_Note: Remember to restart the AppDaemon addon/server after updating to a new version._**
88

9-
<!--
109
## :pencil2: Features
11-
-->
10+
11+
- Add [`event` sensor listener](https://BASE_URL/controllerx/start/integrations/zigbee2mqtt/#event-state-listen_to-event) to Zigbee2MQTT integration. [ #1090 ]
12+
- Deprecate [HA sensor action listener](https://BASE_URL/controllerx/start/integrations/zigbee2mqtt/#ha-states-listen_to-ha) from Zigbee2MQTT integration. [ #1090 ]
1213

1314
<!--
1415
## :video_game: New devices
@@ -20,9 +21,9 @@ _This minor change does not contain any breaking changes._
2021
## :hammer: Fixes
2122
-->
2223

23-
<!--
2424
## :scroll: Docs
25-
-->
25+
26+
- Refactor [Integration page](https://BASE_URL/controllerx/start/integrations).
2627

2728
<!--
2829
## :clock2: Performance

apps/controllerx/cx_core/controller.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ def get_default_actions_mapping(
228228
) -> DefaultActionsMapping:
229229
actions_mapping = integration.get_default_actions_mapping()
230230
if actions_mapping is None:
231-
raise ValueError(f"This controller does not support {integration.name}.")
231+
raise ValueError(
232+
f"This controller does not support {integration.name}. Use `mapping` to define the actions."
233+
)
232234
return actions_mapping
233235

234236
@overload

apps/controllerx/cx_core/integration/tasmota.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def listen_changes(self, controller_id: str) -> None:
1717
if component_key is None:
1818
raise ValueError(
1919
"`component` attribute is mandatory. "
20-
"Check example from https://xaviml.github.io/controllerx/start/integrations/#tasmota"
20+
"Check example from https://xaviml.github.io/controllerx/start/integrations/tasmota"
2121
)
2222
await Mqtt.listen_event(
2323
self.controller, self.event_callback, topic=controller_id, namespace="mqtt"

apps/controllerx/cx_core/integration/z2m.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
LISTENS_TO_HA = "ha"
1010
LISTENS_TO_MQTT = "mqtt"
11+
LISTENS_TO_EVENT = "event"
1112

1213

1314
class Z2MIntegration(Integration):
@@ -19,6 +20,12 @@ def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
1920
async def listen_changes(self, controller_id: str) -> None:
2021
listens_to = self.kwargs.get("listen_to", LISTENS_TO_HA)
2122
if listens_to == LISTENS_TO_HA:
23+
self.controller.log(
24+
"⚠️ Listening to HA sensor actions is now deprecated and will be removed in the future. Use `listen_to: mqtt` or `listen_to: event` instead."
25+
" Read more about it here: https://xaviml.github.io/controllerx/others/z2m-ha-sensor-deprecated",
26+
level="WARNING",
27+
ascii_encode=False,
28+
)
2229
await Hass.listen_state(self.controller, self.state_callback, controller_id)
2330
elif listens_to == LISTENS_TO_MQTT:
2431
topic_prefix = self.kwargs.get("topic_prefix", "zigbee2mqtt")
@@ -28,9 +35,16 @@ async def listen_changes(self, controller_id: str) -> None:
2835
topic=f"{topic_prefix}/{controller_id}",
2936
namespace="mqtt",
3037
)
38+
elif listens_to == LISTENS_TO_EVENT:
39+
await Hass.listen_state(
40+
self.controller,
41+
self.state_callback,
42+
f"event.{controller_id}",
43+
attribute="event_type",
44+
)
3145
else:
3246
raise ValueError(
33-
"`listen_to` has to be either `ha` or `mqtt`. Default is `ha`."
47+
"`listen_to` has to be either `ha`, `mqtt` or `event`. Default is `ha`."
3448
)
3549

3650
async def event_callback(

docs/docs/advanced/entity-groups.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Entity groups
33
layout: page
44
---
55

6-
_This is supported from ControllerX v4.14.0_
6+
_This is supported since ControllerX v4.14.0_
77

88
ControllerX allow for Entity Controllers (LightController, MediaPlayerController, CoverController, etc) to work with grouped entities.
99

docs/docs/advanced/event-integration.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ title: Event integration
33
layout: page
44
---
55

6-
_This is supported from ControllerX v4.23.0_
6+
_This is supported since ControllerX v4.23.0_
77

8-
Most of the integrations supported by ControllerX are defined for an specific use case (zigbee2mqtt, deCONZ, ZHA), but we also have the [`State` integration](/controllerx/start/integrations#state), which is a more generic integration and allows us to listen to any Home Assistant entity state and build a mapping from it. From ControllerX v4.23.0, we can also use the [`Event` integration](/controllerx/start/integrations#event) which allows us define the event we want to listen to, and which actions build from it.
8+
Most of the integrations supported by ControllerX are defined for an specific use case (zigbee2mqtt, deCONZ, ZHA), but we also have the [`State` integration](/controllerx/start/integrations/state), which is a more generic integration and allows us to listen to any Home Assistant entity state and build a mapping from it. From ControllerX v4.23.0, we can also use the [`Event` integration](/controllerx/start/integrations/event) which allows us define the event we want to listen to, and which actions build from it.
99

1010
Each event has its own payload that could look like:
1111

docs/docs/faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ This is probably happenning to you sometimes and is because the stop/release act
4646
However, these are some actions you can take to overcome this problem and reduce the number of times that this happens:
4747

4848
- If `release` action is sent at a similar time than a `hold` one, you could use [`release_delay` attribute](/controllerx/start/type-configuration/?h=release_delay#light-controller) to execute `release` actions some amount of time defined after they have been fired. For example: `release_delay: 0.01` (time in seconds).
49-
- If using z2m, change the integration to listen MQTT directly, this way it will avoid the HA state machine layer. Read more about in [here](/controllerx/start/integrations#zigbee2mqtt).
49+
- If using z2m, change the integration to listen MQTT directly, this way it will avoid the HA state machine layer. Read more about in [here](/controllerx/start/integrations/zigbee2mqtt).
5050
- If using deCONZ and you just want to dim your lights smoothly, then you can consider using [this AppDaemon app](https://github.com/Burningstone91/Hue_Dimmer_Deconz) from [_@Burningstone91_](https://github.com/Burningstone91). It brightens/dims your lights with a deCONZ calls instead of calling HA periodically, this means that deCONZ would handle the dimming for you.
5151
- Play around with delay (default is 350ms) and automatic_steps (default is 10) attributes. You can read more about them in [here](/controllerx/start/type-configuration#light-controller). The lower the delay is, the more requests will go to HA. The more automatic_steps, the more steps it will take to get from min to max, and vice versa.
5252
- Add more Zigbee routers to the network.

docs/docs/others/enable-mqtt-plugin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ layout: page
55

66
If we want to use the `mqtt` integration or the `listen_to: mqtt` from `z2m` integration as well as the `Z2MLightController`, we will need to activate the MQTT Plugin on the AppDaemon configuration (normally located in `/addon_configs/a0d7b954_appdaemon/appdaemon.yaml`). We will need the add the following highlighted section in that file:
77

8-
```yaml hl_lines="12 13 14 15 16 17"
8+
```yaml hl_lines="13-18"
99
---
1010
secrets: /homeassistant/secrets.yaml
1111
appdaemon:

docs/docs/others/extract-controller-id.md

-38
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
title: Zigbee2MQTT - HA sensor is deprecated
3+
layout: page
4+
---
5+
6+
_Zigbee2MQTT HA sensor deprecated since ControllerX v4.29.0_
7+
8+
Zigbee2MQTT 2.0.0 brought [some breaking changes](https://github.com/Koenkk/zigbee2mqtt/discussions/24198), and one of them was to deprecate the [Home Assistant action sensors](https://www.zigbee2mqtt.io/guide/usage/integrations/home_assistant.html#via-home-assistant-action-sensor-deprecated), which is the default option for [Zigbee2MQTT integration](/controllerx/start/integrations/zigbee2mqtt) in ControllerX.
9+
10+
You might be here because of a warning in AppDaemon logs that look like the following:
11+
12+
!!! quote
13+
14+
⚠️ Listening to HA sensor actions is now deprecated and will be removed in the future. Use `listen_to: mqtt` or `listen_to: event` instead. Read more about it here: https://xaviml.github.io/controllerx/others/z2m-ha-sensor-deprecated
15+
16+
This might be because your controller configuration looks like:
17+
18+
```yaml hl_lines="4-5"
19+
livingroom_controller:
20+
module: controllerx
21+
class: E1810Controller
22+
controller: sensor.livingroom_controller_action
23+
integration: z2m
24+
light: light.livingroom
25+
```
26+
27+
The issue is within the `integration: z2m` which is defaulted to read a HA `sensor`. You need to switch to either [`mqtt`](/controllerx/start/integrations/zigbee2mqtt/#mqtt-topics-listen_to-mqtt) (recommended) or HA [`event`](/controllerx/start/integrations/zigbee2mqtt/#event-state-listen_to-event) (experimental).
28+
29+
## Switch to MQTT listener (recommended)
30+
31+
In case of switching to MQTT (as [recommneded by Zigbee2MQTT](https://www.zigbee2mqtt.io/guide/usage/integrations/home_assistant.html#via-mqtt-device-trigger-recommended)), you would need to first [enable MQTT plugin](/controllerx/others/enable-mqtt-plugin). Then, change your configuration to something like the following:
32+
33+
```yaml hl_lines="4-7"
34+
livingroom_controller:
35+
module: controllerx
36+
class: E1810Controller
37+
controller: livingroom_controller # (1)
38+
integration:
39+
name: z2m
40+
listen_to: mqtt # (2)
41+
light: light.livingroom
42+
```
43+
44+
1. This is the device friendly_name in Zigbee2MQTT. Check [here](/controllerx/start/integrations/zigbee2mqtt/#mqtt-topics-listen_to-mqtt) how to get this value.
45+
2. By indicating `mqtt` here, ControllerX will listen to MQTT controller topic.
46+
47+
## Switching to HA Event sensor listener (experimental)
48+
49+
Another option is to listen the newly (and experimental) event entity from Zigbee2MQTT 2.0.0. First, we will need to enable the experimental feature in Zigbee2MQTT as explained [here](/controllerx/start/integrations/zigbee2mqtt/#event-state-listen_to-event). Then, you would need to switch configuration to something like:
50+
51+
```yaml hl_lines="4-7"
52+
livingroom_controller:
53+
module: controllerx
54+
class: E1810Controller
55+
controller: office_controller_action # (1)
56+
integration:
57+
name: z2m
58+
listen_to: event # (2)
59+
light: light.livingroom
60+
```
61+
62+
1. This is the event entity without the `event.` prefix. Check [here](/controllerx/start/integrations/zigbee2mqtt/#event-state-listen_to-event) how to get this value.
63+
2. By indicating `event` here, ControllerX will listen to the event entity changes.

docs/docs/others/zigbee2mqtt-light-controller.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Zigbee2MQTT Light Controller
33
layout: page
44
---
55

6-
_This is supported from ControllerX v4.21.0_
6+
_This is supported since ControllerX v4.21.0_
77

88
ControllerX has always given support for [Light Controller](/controllerx/start/type-configuration#light-controller) which allows amongst other features to smoothly change attributes (brightness, color temperature) values by requesting the changes periodically to Home Assistant. This has allowed to work with lights integrated with many integrations (e.g.: Zigbee2MQTT, deCONZ, WLED, Hue). However, this generalization has penalized Zigbee2MQTT which has its own mechanism to change brightness and color temp over time which works much smoother than the Light Controller.
99

0 commit comments

Comments
 (0)