Skip to content

Commit

Permalink
feat(core): add transition only for click events in z2m light controller
Browse files Browse the repository at this point in the history
related to #168
  • Loading branch information
xaviml committed May 30, 2022
1 parent 9fe0893 commit 073893f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 46 deletions.
35 changes: 10 additions & 25 deletions apps/controllerx/cx_core/type/z2m_light_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import json
from functools import lru_cache
from typing import Any, Dict, List, Optional, Set, Type
from typing import Any, Dict, Optional, Set, Type

from cx_const import PredefinedActionsMapping, StepperDir, Z2MLight
from cx_core.controller import action
Expand All @@ -14,26 +14,8 @@
DEFAULT_HOLD_STEPS = 70
DEFAULT_TRANSITION = 0.5

Mode = str
# Once the minimum supported version of Python is 3.8,
# we can declare the Mode as a Literal
# ColorMode = Literal["ha", "mqtt"]


class Z2MLightEntity(Entity):
mode: Mode

def __init__(
self,
name: str,
entities: Optional[List[str]] = None,
color_mode: Mode = "ha",
) -> None:
super().__init__(name, entities)
self.color_mode = color_mode


class Z2MLightController(TypeController[Z2MLightEntity]):
class Z2MLightController(TypeController[Entity]):
"""
This is the main class that controls the Zigbee2MQTT lights for different devices.
Type of actions:
Expand Down Expand Up @@ -72,8 +54,8 @@ async def init(self) -> None:

await super().init()

def _get_entity_type(self) -> Type[Z2MLightEntity]:
return Z2MLightEntity
def _get_entity_type(self) -> Type[Entity]:
return Entity

def get_predefined_actions_mapping(self) -> PredefinedActionsMapping:
return {
Expand Down Expand Up @@ -254,18 +236,21 @@ async def _change_light_state(
attribute: str,
direction: str,
stepper: InvertStepper,
transition: float,
transition: Optional[float],
use_onoff: bool,
mode: str,
) -> None:
onoff_cmd = (
"_onoff" if use_onoff and attribute == self.ATTRIBUTE_BRIGHTNESS else ""
)
stepper_output = stepper.step(stepper.steps, direction)
kwargs = {}
if transition is not None:
kwargs["transition"] = transition
await self._mqtt_call(
{
f"{attribute}_{mode}{onoff_cmd}": stepper_output.next_value,
"transition": transition,
**kwargs,
}
)

Expand Down Expand Up @@ -313,7 +298,7 @@ async def _hold(
attribute=attribute,
direction=direction,
stepper=stepper,
transition=self.transition,
transition=None,
use_onoff=use_onoff if use_onoff is not None else self.use_onoff,
mode="move",
)
Expand Down
23 changes: 7 additions & 16 deletions docs/docs/start/type-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,13 @@ This controller (`Z2MLightController`) allows the devices to control Zigbe2MQTT
- Manual increase/decrease of brightness and color
- Smooth increase/decrease (holding button) of brightness and color

| 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 | `my_light` | The light you want to control. This is the friendly name light from Zigbee2MQTT. |
| `mode` | string | `ha` | This attribute only takes `ha` or `mqtt` as values. `ha` will send the mqtt messages through `mqtt.publish` service call. `mqtt` will send the mqtt message through AppDaemon MQTT plugin (read more about it [here](/controllerx/start/integrations#mqtt)). By default is `ha`. |
| key | type | value | description |
| ------------- | ------ | ---------- | ------------------------------------------------------------------------------------------------------ |
| `light`\* | string | `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_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ expected_calls:
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"brightness_move_onoff": -70, "transition": 0.5}'
payload: '{"brightness_move_onoff": -70}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
Expand Down
2 changes: 1 addition & 1 deletion tests/integ_tests/z2m_light_controller/hold_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ expected_calls:
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"brightness_move": 70, "transition": 0.5}'
payload: '{"brightness_move": 70}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
Expand Down
4 changes: 2 additions & 2 deletions tests/integ_tests/z2m_light_controller/hold_toggle_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ expected_calls:
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"color_temp_move": 70, "transition": 0.5}'
payload: '{"color_temp_move": 70}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
Expand All @@ -22,7 +22,7 @@ expected_calls:
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"color_temp_move": -70, "transition": 0.5}'
payload: '{"color_temp_move": -70}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ expected_calls:
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
payload: '{"brightness_move": -70, "transition": 0.5}'
payload: '{"brightness_move": -70}'
- service: mqtt/publish
data:
topic: zigbee2mqtt/livingroom_lamp/set
Expand Down

0 comments on commit 073893f

Please sign in to comment.