Skip to content

Commit

Permalink
Merge pull request #513 from xaviml/fix/z2m-light-controller
Browse files Browse the repository at this point in the history
fix(z2m-light-controller): send only 1 release attribute mqtt topic
  • Loading branch information
xaviml authored Jun 14, 2022
2 parents 26e8b80 + 477c5bb commit a9fd977
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 34 deletions.
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ repos:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
exclude: ^docs/mkdocs.yml
exclude: ^docs/mkdocs.yml$
- id: debug-statements
- id: name-tests-test
exclude: ^tests/test_utils.py$
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.9.0
hooks:
Expand Down
19 changes: 12 additions & 7 deletions apps/controllerx/cx_core/type/z2m_light_controller.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import json
from functools import lru_cache
from typing import Any, Awaitable, Callable, Dict, List, Optional, Type
Expand Down Expand Up @@ -68,6 +67,8 @@ class Z2MLightController(TypeController[Z2MLightEntity]):
transition: float
use_onoff: bool

hold_attribute: Optional[str]

_mqtt_fn: Dict[Mode, Callable[[str, str], Awaitable[None]]]

async def init(self) -> None:
Expand All @@ -80,6 +81,7 @@ async def init(self) -> None:
"ha": self._ha_mqtt_call,
"mqtt": self._mqtt_plugin_call,
}
self.hold_attribute = None

await super().init()

Expand Down Expand Up @@ -200,6 +202,10 @@ def get_predefined_actions_mapping(self) -> PredefinedActionsMapping:
Z2MLight.BRIGHTNESS_FROM_CONTROLLER_ANGLE: self.brightness_from_controller_angle,
}

async def before_action(self, action: str, *args: Any, **kwargs: Any) -> bool:
to_return = not (action == "hold" and self.hold_attribute is not None)
return await super().before_action(action, *args, **kwargs) and to_return

async def _ha_mqtt_call(self, topic: str, payload: str) -> None:
await self.call_service("mqtt.publish", topic=topic, payload=payload)

Expand Down Expand Up @@ -329,6 +335,7 @@ async def _hold(
steps = steps if steps is not None else self.hold_steps
stepper = self.get_stepper(attribute, steps, tag="hold")
direction = stepper.get_direction(steps, direction)
self.hold_attribute = attribute
await self._change_light_state(
attribute=attribute,
direction=direction,
Expand All @@ -350,12 +357,10 @@ async def hold(

@action
async def release(self) -> None:
await asyncio.gather(
*[
self._mqtt_call({f"{attribute}_move": "stop"})
for attribute in self.ATTRIBUTES_LIST
]
)
if self.hold_attribute is None:
return
await self._mqtt_call({f"{self.hold_attribute}_move": "stop"})
self.hold_attribute = None

@action
async def xycolor_from_controller(self, extra: Optional[EventData] = None) -> None:
Expand Down
4 changes: 0 additions & 4 deletions tests/integ_tests/z2m_light_controller/hold_onoff_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ expected_calls:
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"brightness_move": "stop"}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"color_temp_move": "stop"}'
13 changes: 8 additions & 5 deletions tests/integ_tests/z2m_light_controller/hold_test.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
fired_actions: [brightness_up_hold, 0.450, brightness_up_release]
fired_actions:
- brightness_up_hold
- 0.01
- brightness_up_hold
- 0.450
- brightness_up_release
- 0.01
- brightness_down_release
expected_calls:
- service: mqtt/publish
data:
Expand All @@ -8,7 +15,3 @@ expected_calls:
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"brightness_move": "stop"}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"color_temp_move": "stop"}'
8 changes: 0 additions & 8 deletions tests/integ_tests/z2m_light_controller/hold_toggle_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ expected_calls:
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"color_temp_move": 70}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"brightness_move": "stop"}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
Expand All @@ -23,10 +19,6 @@ expected_calls:
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"color_temp_move": -70}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"brightness_move": "stop"}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fired_actions: [brightness_down_release]
expected_calls_count: 0
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,3 @@ expected_calls:
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"brightness_move": "stop"}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"color_temp_move": "stop"}'
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,3 @@ expected_calls:
topic: z2m/livingroom_lamp/set
payload: '{"brightness_move": "stop"}'
namespace: mqtt
- service: mqtt/publish
data:
topic: z2m/livingroom_lamp/set
payload: '{"color_temp_move": "stop"}'
namespace: mqtt

0 comments on commit a9fd977

Please sign in to comment.