Skip to content

Commit

Permalink
fix(device): fix bug with ZNXNKG02LMLightController when rotating
Browse files Browse the repository at this point in the history
related to #430
  • Loading branch information
xaviml committed Apr 22, 2022
1 parent 957f729 commit 1e47f45
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 34 deletions.
2 changes: 1 addition & 1 deletion apps/controllerx/cx_core/type/light_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,7 @@ def check_smooth_power_on(

async def before_action(self, action: str, *args: Any, **kwargs: Any) -> bool:
to_return = True
self.next_direction = None
if action in ("click", "hold"):
if len(args) == 2:
attribute, direction = args
Expand All @@ -689,7 +690,6 @@ async def before_action(self, action: str, *args: Any, **kwargs: Any) -> bool:
self.remove_transition_check = await self.check_remove_transition(
on_from_user=False
)
self.next_direction = None
to_return = (light_state == "on") or self.smooth_power_on_check
else:
self.remove_transition_check = await self.check_remove_transition(
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
entity_state_attributes:
brightness: 100.8
entity_state: "on"
fired_actions:
- start_rotating
- 0.450
- stop_rotating
- 0.3
- start_rotating
- 0.450
- stop_rotating
extra: [{ action_rotation_angle: -42 }, { action_rotation_angle: 42 }]
expected_calls:
- service: light/turn_on
data:
entity_id: light.my_light
brightness: 75.4
- service: light/turn_on
data:
entity_id: light.my_light
brightness: 50
# Now we expect the brightness to go up from the original brightness (100.8)
- service: light/turn_on
data:
entity_id: light.my_light
brightness: 126.2
- service: light/turn_on
data:
entity_id: light.my_light
brightness: 151.6
46 changes: 43 additions & 3 deletions tests/integ_tests/integ_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import asyncio
import glob
from pathlib import Path
from typing import Any, Awaitable, Callable, Dict, List, Optional, Set, Tuple
from typing import (
Any,
Awaitable,
Callable,
Dict,
Iterator,
List,
Optional,
Set,
Tuple,
Union,
)

import pytest
import yaml
Expand Down Expand Up @@ -43,6 +54,35 @@ async def inner(entity_name: str, attribute: Optional[str] = None) -> str:
integration_tests = get_integ_tests()


class ExtraIterator:
iterator: Iterator[Optional[Dict[str, Any]]]
current: Optional[Dict[str, Any]] = None

def __init__(self, iterator: Iterator[Optional[Dict[str, Any]]]) -> None:
self.iterator = iterator

def __next__(self) -> Optional[Dict[str, Any]]:
try:
self.current = next(self.iterator)
except StopIteration:
# Once iterator is finished, we will return always current
# which is the last element from the iterator
pass
finally:
return self.current


def _get_extra(
data: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]]
) -> ExtraIterator:
if data is None:
return ExtraIterator(iter([None]))
if isinstance(data, list):
return ExtraIterator(iter(data))
elif isinstance(data, dict):
return ExtraIterator(iter([data]))


@pytest.mark.parametrize("config_file, test_yaml_file, data", integration_tests)
async def test_integ_configs(
mocker: MockerFixture, config_file: str, test_yaml_file: str, data: Dict[str, Any]
Expand All @@ -52,7 +92,7 @@ async def test_integ_configs(
previous_state = data.get("previous_state", None)
fired_actions = data.get("fired_actions", [])
render_template_response = data.get("render_template_response")
extra = data.get("extra")
extras: ExtraIterator = _get_extra(data.get("extra"))
expected_calls = data.get("expected_calls", [])
expected_calls_count = data.get("expected_calls_count", len(expected_calls))

Expand Down Expand Up @@ -80,7 +120,7 @@ async def test_integ_configs(
for idx, action in enumerate(fired_actions):
if any(isinstance(action, type_) for type_ in (str, int)):
coroutine = controller.handle_action(
action, previous_state=previous_state, extra=extra
action, previous_state=previous_state, extra=next(extras)
)
if idx == len(fired_actions) - 1:
await coroutine
Expand Down

0 comments on commit 1e47f45

Please sign in to comment.