Skip to content

Commit

Permalink
feat(z2m-light-controller): add "base_topic" attribute
Browse files Browse the repository at this point in the history
related to #118, #168
  • Loading branch information
xaviml committed Jun 8, 2022
1 parent d5df1fa commit 9f6b0dd
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
4 changes: 3 additions & 1 deletion apps/controllerx/cx_core/type/z2m_light_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
DEFAULT_CLICK_STEPS = 70
DEFAULT_HOLD_STEPS = 70
DEFAULT_TRANSITION = 0.5
DEFAULT_BASE_TOPIC = "zigbee2mqtt"

# Once the minimum supported version of Python is 3.8,
# we can declare the Mode as a Literal
Expand Down Expand Up @@ -70,6 +71,7 @@ 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 @@ -205,7 +207,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"zigbee2mqtt/{self.entity.name}/set", json.dumps(payload)
f"{self.base_topic}/{self.entity.name}/set", json.dumps(payload)
)

async def _on(self, **attributes: Any) -> None:
Expand Down
15 changes: 8 additions & 7 deletions docs/docs/start/type-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ 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. |
| `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. |
| `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. |

_\* Required fields_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ fired_actions: [arrow_left_click]
expected_calls:
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
topic: z2m/livingroom_lamp/set
payload: '{"color_temp_step": -70, "transition": 0.5}'
namespace: mqtt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ example_app:
class: E1810Z2MLightController
integration: z2m
controller: sensor.livingroom_controller_action
base_topic: z2m
light:
name: livingroom_lamp
mode: mqtt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ fired_actions: [brightness_up_hold, 0.450, brightness_up_release]
expected_calls:
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
topic: z2m/livingroom_lamp/set
payload: '{"brightness_move": 70}'
namespace: mqtt
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
topic: z2m/livingroom_lamp/set
payload: '{"brightness_move": "stop"}'
namespace: mqtt
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
topic: z2m/livingroom_lamp/set
payload: '{"color_temp_move": "stop"}'
namespace: mqtt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ fired_actions: [toggle]
expected_calls:
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
topic: z2m/livingroom_lamp/set
payload: '{"state": "TOGGLE"}'
namespace: mqtt

0 comments on commit 9f6b0dd

Please sign in to comment.