Skip to content

Commit

Permalink
Merge pull request #145 from xaviml/toggle_advanced
Browse files Browse the repository at this point in the history
`toggle_full` and `toggle_min` new predefined actions
  • Loading branch information
xaviml authored Sep 29, 2020
2 parents 3db3371 + bf7a262 commit b75d6fd
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 2 deletions.
6 changes: 6 additions & 0 deletions apps/controllerx/cx_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class Light:
ON = "on"
OFF = "off"
TOGGLE = "toggle"
TOGGLE_FULL_BRIGHTNESS = "toggle_full_brightness"
TOGGLE_FULL_WHITE_VALUE = "toggle_full_white_value"
TOGGLE_FULL_COLOR_TEMP = "toggle_full_color_temp"
TOGGLE_MIN_BRIGHTNESS = "toggle_min_brightness"
TOGGLE_MIN_WHITE_VALUE = "toggle_min_white_value"
TOGGLE_MIN_COLOR_TEMP = "toggle_min_color_temp"
RELEASE = "release"
ON_FULL_BRIGHTNESS = "on_full_brightness"
ON_FULL_WHITE_VALUE = "on_full_white_value"
Expand Down
40 changes: 38 additions & 2 deletions apps/controllerx/cx_core/type/light_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ def get_type_actions_mapping(self,) -> TypeActionsMapping:
Light.ON: self.on,
Light.OFF: self.off,
Light.TOGGLE: self.toggle,
Light.TOGGLE_FULL_BRIGHTNESS: (
self.toggle_full,
LightController.ATTRIBUTE_BRIGHTNESS,
),
Light.TOGGLE_FULL_WHITE_VALUE: (
self.toggle_full,
LightController.ATTRIBUTE_WHITE_VALUE,
),
Light.TOGGLE_FULL_COLOR_TEMP: (
self.toggle_full,
LightController.ATTRIBUTE_COLOR_TEMP,
),
Light.TOGGLE_MIN_BRIGHTNESS: (
self.toggle_min,
LightController.ATTRIBUTE_BRIGHTNESS,
),
Light.TOGGLE_MIN_WHITE_VALUE: (
self.toggle_min,
LightController.ATTRIBUTE_WHITE_VALUE,
),
Light.TOGGLE_MIN_COLOR_TEMP: (
self.toggle_min,
LightController.ATTRIBUTE_COLOR_TEMP,
),
Light.RELEASE: self.release,
Light.ON_FULL_BRIGHTNESS: (
self.on_full,
Expand Down Expand Up @@ -300,9 +324,9 @@ async def call_light_service(
if "transition" not in attributes:
attributes["transition"] = self.transition / 1000
if (
await self.supported_features.not_supported(LightSupport.TRANSITION)
or not self.add_transition
not self.add_transition
or (turned_toggle and not self.add_transition_turn_toggle)
or await self.supported_features.not_supported(LightSupport.TRANSITION)
):
del attributes["transition"]
await self.call_service(service, entity_id=self.light["name"], **attributes)
Expand Down Expand Up @@ -338,6 +362,18 @@ async def set_value(
value = (max_ - min_) * fraction + min_
await self.on(light_on=light_on, **{attribute: value})

@action
async def toggle_full(self, attribute: str) -> None:
stepper = self.automatic_steppers[attribute]
if isinstance(stepper, MinMaxStepper):
await self.toggle(**{attribute: stepper.minmax.max})

@action
async def toggle_min(self, attribute: str) -> None:
stepper = self.automatic_steppers[attribute]
if isinstance(stepper, MinMaxStepper):
await self.toggle(**{attribute: stepper.minmax.min})

@action
async def on_full(self, attribute: str, light_on: bool = None) -> None:
await self.set_value(attribute, 1, light_on=light_on)
Expand Down
6 changes: 6 additions & 0 deletions docs/others/custom-controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ This controller lets you map controller events with predefined light actions. Th
| `on` | It turns on the light |
| `off` | It turns off the light |
| `toggle` | It toggles the light |
| `toggle_full_brightness` | It toggles the light, setting the brightness to the maximum value when turning on. |
| `toggle_full_white_value` | It toggles the light, setting the white value to the maximum value when turning on. |
| `toggle_full_color_temp` | It toggles the light, setting the color temperature to the maximum value when turning on. |
| `toggle_min_brightness` | It toggles the light, setting the brightness to the minimum value when turning on. |
| `toggle_min_white_value` | It toggles the light, setting the white value to the minimum value when turning on. |
| `toggle_min_color_temp` | It toggles the light, setting the color temperature to the minimum value when turning on. |
| `release` | It stops `hold` actions |
| `on_full_brightness` | It puts the brightness to the maximum value |
| `on_full_white_value` | It puts the white value to the maximum value |
Expand Down
15 changes: 15 additions & 0 deletions tests/integ_tests/toggle_full_and_min/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
example_app:
module: controllerx
class: E1810Controller
controller: my_controller
integration:
name: z2m
listen_to: mqtt
light: light.my_light
min_color_temp: 100
max_color_temp: 300
max_brightness: 255
mapping:
toggle: toggle_full_brightness
brightness_up_click: toggle_full_color_temp
brightness_down_click: toggle_min_color_temp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [toggle]
expected_calls:
- service: light/toggle
data:
entity_id: light.my_light
brightness: 255

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [brightness_up_click]
expected_calls:
- service: light/toggle
data:
entity_id: light.my_light
color_temp: 300

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
entity_state_attributes:
supported_features: 191
entity_state: "on"
fired_actions: [brightness_down_click]
expected_calls:
- service: light/toggle
data:
entity_id: light.my_light
color_temp: 100

48 changes: 48 additions & 0 deletions tests/unit_tests/cx_core/type/light_controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ async def fake_get_entity_state(*args, **kwargs):
sut.manual_steppers = {attribute: stepper}
sut.automatic_steppers = {attribute: stepper}
sut.transition = 300
sut.add_transition = True
sut.add_transition_turn_toggle = False
sut.supported_features = LightSupport(None, None, False)
sut.supported_features._supported_features = set()
sut.color_wheel = get_color_wheel("default_color_wheel")
Expand Down Expand Up @@ -354,6 +356,52 @@ async def test_toggle(sut, mocker, monkeypatch):
)


@pytest.mark.parametrize(
"attribute, stepper, expected_attribute_value",
[
("brightness", MinMaxStepper(1, 255, 1), 255),
("color_temp", MinMaxStepper(153, 500, 1), 500),
("test", MinMaxStepper(1, 10, 1), 10),
],
)
@pytest.mark.asyncio
async def test_toggle_full(sut, mocker, attribute, stepper, expected_attribute_value):
sut.light = {"name": "test_light"}
sut.transition = 300
sut.add_transition = False
call_service_patch = mocker.patch.object(sut, "call_service")
sut.automatic_steppers = {attribute: stepper}

await sut.toggle_full(attribute)
call_service_patch.assert_called_once_with(
"light/toggle",
**{"entity_id": "test_light", attribute: expected_attribute_value}
)


@pytest.mark.parametrize(
"attribute, stepper, expected_attribute_value",
[
("brightness", MinMaxStepper(1, 255, 1), 1),
("color_temp", MinMaxStepper(153, 500, 1), 153),
("test", MinMaxStepper(1, 10, 1), 1),
],
)
@pytest.mark.asyncio
async def test_toggle_min(sut, mocker, attribute, stepper, expected_attribute_value):
sut.light = {"name": "test_light"}
sut.transition = 300
sut.add_transition = False
call_service_patch = mocker.patch.object(sut, "call_service")
sut.automatic_steppers = {attribute: stepper}

await sut.toggle_min(attribute)
call_service_patch.assert_called_once_with(
"light/toggle",
**{"entity_id": "test_light", attribute: expected_attribute_value}
)


@pytest.mark.parametrize(
"stepper_cls, min_max, fraction, expected_calls, expected_value",
[
Expand Down

0 comments on commit b75d6fd

Please sign in to comment.