Skip to content

Commit

Permalink
Merge pull request #510 from xaviml/fix/topic-prefix
Browse files Browse the repository at this point in the history
fix: move base_topic to Z2MLightEntity and rename to topic_prefix
  • Loading branch information
xaviml authored Jun 9, 2022
2 parents 225bd22 + bb01965 commit c30d7e6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
11 changes: 7 additions & 4 deletions apps/controllerx/cx_core/type/z2m_light_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
DEFAULT_CLICK_STEPS = 70
DEFAULT_HOLD_STEPS = 70
DEFAULT_TRANSITION = 0.5
DEFAULT_BASE_TOPIC = "zigbee2mqtt"
DEFAULT_MODE = "ha"
DEFAULT_TOPIC_PREFIX = "zigbee2mqtt"

# Once the minimum supported version of Python is 3.8,
# we can declare the Mode as a Literal
Expand All @@ -23,16 +24,19 @@

class Z2MLightEntity(Entity):
mode: Mode
topic_prefix: str

def __init__(
self,
name: str,
entities: Optional[List[str]] = None,
mode: Mode = "ha",
mode: Mode = DEFAULT_MODE,
topic_prefix: str = DEFAULT_TOPIC_PREFIX,
) -> None:
super().__init__(name, entities)
mode = Controller.get_option(mode, ["ha", "mqtt"])
self.mode = mode
self.topic_prefix = topic_prefix


class Z2MLightController(TypeController[Z2MLightEntity]):
Expand Down Expand Up @@ -71,7 +75,6 @@ async def init(self) -> None:
self.hold_steps = self.args.get("hold_steps", DEFAULT_HOLD_STEPS)
self.transition = self.args.get("transition", DEFAULT_TRANSITION)
self.use_onoff = self.args.get("use_onoff", False)
self.base_topic = self.args.get("base_topic", DEFAULT_BASE_TOPIC)

self._mqtt_fn = {
"ha": self._ha_mqtt_call,
Expand Down Expand Up @@ -207,7 +210,7 @@ async def _mqtt_plugin_call(self, topic: str, payload: str) -> None:

async def _mqtt_call(self, payload: Dict[str, Any]) -> None:
await self._mqtt_fn[self.entity.mode](
f"{self.base_topic}/{self.entity.name}/set", json.dumps(payload)
f"{self.entity.topic_prefix}/{self.entity.name}/set", json.dumps(payload)
)

async def _on(self, **attributes: Any) -> None:
Expand Down
16 changes: 9 additions & 7 deletions docs/docs/start/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ Integrations is a way to abstract the logic from the event extraction in Control

#### Zigbee2MQTT

This integration(**`z2m`**) is meant to be used for zigbee2mqtt. It listens the states from the HA sensor entities. You can add `listen_to` attribute to indicate if it listens for HA states (`ha`) or MQTT topics (`mqtt`). Default is `ha`. If you want to use the `mqtt`, then you will need to change the `appdaemon.yaml` as it is stated in the `MQTT` integration section. Imagine we have the following configuration already created for a `z2m` controller listening to HA state:
This integration(**`z2m`**) is meant to be used for zigbee2mqtt. It listens the states from the HA sensor entities. These are the accepted attributes:

- `listen_to`: Indicates whether it listens for HA states (`ha`) or MQTT topics (`mqtt`). Default is `ha`.
- `action_key`: It is the key inside the topic payload that contains the fired action from the controller. It is normally `action` or `click`. By default will be `action`. Only applicable is `listen_to` is `mqtt`.
- `action_group` is a list of allowed action groups for the controller configuration. Read more about it [here](https://github.com/xaviml/controllerx/pull/150). Only applicable is `listen_to` is `mqtt`.
- `topic_prefix`: MQTT base topic for Zigbee2MQTT MQTT messages. By default is `zigbee2mqtt`, and it listens to `<topic_prefix>/<controller_id>` for changes. Only applicable is `listen_to` is `mqtt`.

If you want to use the `mqtt`, then you will need to change the `appdaemon.yaml` as it is stated in the `MQTT` integration section. Imagine we have the following configuration already created for a `z2m` controller listening to HA state:

```yaml
livingroom_controller:
Expand All @@ -32,12 +39,7 @@ livingroom_controller:
light: light.bedroom
```
Three things to clarify when using the `z2m` integration listening to MQTT:

- `appdaemon.yaml` needs to be changed by adding the MQTT plugin (see [here](/controllerx/others/enable-mqtt-plugin))
- The Zigbee2MQTT friendly name from the z2m needs to be specified in the `controller` attribute.
- `action_key` is the key inside the topic payload that contains the fired action from the controller. It is normally `action` or `click`. By default will be `action`.
- `action_group` is a list of allowed action groups for the controller configuration. Read more about it [here](https://github.com/xaviml/controllerx/pull/150).
`appdaemon.yaml` needs to be changed by adding the MQTT plugin (see [here](/controllerx/others/enable-mqtt-plugin))

#### deCONZ

Expand Down
24 changes: 12 additions & 12 deletions docs/docs/start/type-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ This controller (`Z2MLightController`) allows the devices to control Zigbe2MQTT

You can read more about this specific controller [here](/controllerx/others/zigbee2mqtt-light-controller).

| key | type | value | description |
| ------------- | -------------------- | ------------- | --------------------------------------------------------------------------------------------------------- |
| `light`\* | string \| dictionary | `my_light` | The light you want to control. This is the friendly name light from Zigbee2MQTT. |
| `base_topic` | string | `zigbee2mqtt` | MQTT base topic for Zigbee2MQTT MQTT messages. The topic sent will be `<base_topic>/<friendly_name>/set`. |
| `click_steps` | float | 70 | Number of steps that are passed to Zigbee2MQTT for click actions. |
| `hold_steps` | float | 70 | Number of steps that are passed to Zigbee2MQTT for hold actions. |
| `transition` | float | 0.5 | Transition sent to Zigbee2MQTT when changing brightness or color temp. |
| `use_onoff` | bool | `false` | This allows click and hold actions to turn on/off the light when off or minimum brightness is reached. |
| key | type | value | description |
| ------------- | -------------------- | ---------- | ------------------------------------------------------------------------------------------------------ |
| `light`\* | string \| dictionary | `my_light` | The light you want to control. This is the friendly name light from Zigbee2MQTT. |
| `click_steps` | float | 70 | Number of steps that are passed to Zigbee2MQTT for click actions. |
| `hold_steps` | float | 70 | Number of steps that are passed to Zigbee2MQTT for hold actions. |
| `transition` | float | 0.5 | Transition sent to Zigbee2MQTT when changing brightness or color temp. |
| `use_onoff` | bool | `false` | This allows click and hold actions to turn on/off the light when off or minimum brightness is reached. |

_\* Required fields_

_Light dictionary for the `light` attribute:_

| key | type | value | description |
| -------- | ------ | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`\* | string | `light.kitchen` | The light you want to control. This is the friendly name light from Zigbee2MQTT. |
| `mode` | string | `ha` | This attribute can take `ha`, `mqtt`. On the one hand, `ha` will send the mqtt messages through Home Assistant with [`mqtt.publish` service](https://www.home-assistant.io/docs/mqtt/service/#service-mqttpublish). On the other hand, `mqtt` will send the MQTT messages through MQTT plugin from AppDaemon (hence skipping HA). |
| key | type | value | description |
| -------------- | ------ | --------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`\* | string | `light.kitchen` | The light you want to control. This is the friendly name light from Zigbee2MQTT. |
| `mode` | string | `ha` | This attribute can take `ha`, `mqtt`. On the one hand, `ha` will send the mqtt messages through Home Assistant with [`mqtt.publish` service](https://www.home-assistant.io/docs/mqtt/service/#service-mqttpublish). On the other hand, `mqtt` will send the MQTT messages through MQTT plugin from AppDaemon (hence skipping HA). |
| `topic_prefix` | string | `zigbee2mqtt` | MQTT base topic for Zigbee2MQTT MQTT messages. The topic sent will be `<topic_prefix>/<friendly_name>/set`. |

_\* Required fields_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ example_app:
class: E1810Z2MLightController
integration: z2m
controller: sensor.livingroom_controller_action
base_topic: z2m
light:
name: livingroom_lamp
mode: mqtt
topic_prefix: z2m

0 comments on commit c30d7e6

Please sign in to comment.