Skip to content

Commit

Permalink
test(light_controller): add missing test for new functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Sep 5, 2021
1 parent bc4b5be commit 8d3ade6
Show file tree
Hide file tree
Showing 13 changed files with 385 additions and 203 deletions.
12 changes: 12 additions & 0 deletions apps/controllerx/cx_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,15 @@ class Cover:
STOP = "stop"
TOGGLE_OPEN = "toggle_open"
TOGGLE_CLOSE = "toggle_close"


class StepperDir:
UP = "up"
DOWN = "down"
TOGGLE = "toggle"


class StepperMode:
STOP = "stop"
LOOP = "loop"
BOUNCE = "bounce"
13 changes: 5 additions & 8 deletions apps/controllerx/cx_core/stepper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional

from attr import dataclass
from cx_const import Number
from cx_const import Number, StepperDir


class MinMax:
Expand Down Expand Up @@ -52,18 +52,15 @@ def exceeded(self) -> bool:


class Stepper(abc.ABC):
UP = "up"
DOWN = "down"
TOGGLE = "toggle"
sign_mapping = {UP: 1, DOWN: -1}
sign_mapping = {StepperDir.UP: 1, StepperDir.DOWN: -1}

previous_direction: str = DOWN
previous_direction: str = StepperDir.DOWN
min_max: MinMax
steps: Number

@staticmethod
def invert_direction(direction: str) -> str:
return Stepper.UP if direction == Stepper.DOWN else Stepper.DOWN
return StepperDir.UP if direction == StepperDir.DOWN else StepperDir.DOWN

@staticmethod
def sign(direction: str) -> int:
Expand All @@ -74,7 +71,7 @@ def __init__(self, min_max: MinMax, steps: Number) -> None:
self.steps = steps

def get_direction(self, value: Number, direction: str) -> str:
if direction == Stepper.TOGGLE:
if direction == StepperDir.TOGGLE:
direction = Stepper.invert_direction(self.previous_direction)
self.previous_direction = direction
return direction
Expand Down
10 changes: 5 additions & 5 deletions apps/controllerx/cx_core/stepper/stop_stepper.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from cx_const import Number
from cx_const import Number, StepperDir
from cx_core.stepper import Stepper, StepperOutput


class StopStepper(Stepper):
def get_direction(self, value: Number, direction: str) -> str:
value = self.min_max.clip(value)
if direction == Stepper.TOGGLE and self.min_max.in_min_boundaries(value):
self.previous_direction = Stepper.UP
if direction == StepperDir.TOGGLE and self.min_max.in_min_boundaries(value):
self.previous_direction = StepperDir.UP
return self.previous_direction
if direction == Stepper.TOGGLE and self.min_max.in_max_boundaries(value):
self.previous_direction = Stepper.DOWN
if direction == StepperDir.TOGGLE and self.min_max.in_max_boundaries(value):
self.previous_direction = StepperDir.DOWN
return self.previous_direction
return super().get_direction(value, direction)

Expand Down
82 changes: 45 additions & 37 deletions apps/controllerx/cx_core/type/light_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from functools import lru_cache
from typing import Any, Dict, List, Optional, Set, Type

from cx_const import Light, Number, PredefinedActionsMapping
from cx_const import Light, Number, PredefinedActionsMapping, StepperDir, StepperMode
from cx_core.color_helper import Color, get_color_wheel
from cx_core.controller import action
from cx_core.feature_support.light import LightSupport
Expand Down Expand Up @@ -35,9 +35,9 @@

COLOR_MODES = {"hs", "xy", "rgb", "rgbw", "rgbww"}
STEPPER_MODES: Dict[str, Type[Stepper]] = {
"stop": StopStepper,
"loop": LoopStepper,
"bounce": BounceStepper,
StepperMode.STOP: StopStepper,
StepperMode.LOOP: LoopStepper,
StepperMode.BOUNCE: BounceStepper,
}


Expand Down Expand Up @@ -222,176 +222,176 @@ def get_predefined_actions_mapping(self) -> PredefinedActionsMapping:
self.click,
(
LightController.ATTRIBUTE_BRIGHTNESS,
Stepper.UP,
StepperDir.UP,
),
),
Light.CLICK_BRIGHTNESS_DOWN: (
self.click,
(
LightController.ATTRIBUTE_BRIGHTNESS,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.CLICK_WHITE_VALUE_UP: (
self.click,
(
LightController.ATTRIBUTE_WHITE_VALUE,
Stepper.UP,
StepperDir.UP,
),
),
Light.CLICK_WHITE_VALUE_DOWN: (
self.click,
(
LightController.ATTRIBUTE_WHITE_VALUE,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.CLICK_COLOR_UP: (
self.click,
(
LightController.ATTRIBUTE_COLOR,
Stepper.UP,
StepperDir.UP,
),
),
Light.CLICK_COLOR_DOWN: (
self.click,
(
LightController.ATTRIBUTE_COLOR,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.CLICK_COLOR_TEMP_UP: (
self.click,
(
LightController.ATTRIBUTE_COLOR_TEMP,
Stepper.UP,
StepperDir.UP,
),
),
Light.CLICK_COLOR_TEMP_DOWN: (
self.click,
(
LightController.ATTRIBUTE_COLOR_TEMP,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.CLICK_XY_COLOR_UP: (
self.click,
(
LightController.ATTRIBUTE_XY_COLOR,
Stepper.UP,
StepperDir.UP,
),
),
Light.CLICK_XY_COLOR_DOWN: (
self.click,
(
LightController.ATTRIBUTE_XY_COLOR,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.HOLD: self.hold,
Light.HOLD_BRIGHTNESS_UP: (
self.hold,
(
LightController.ATTRIBUTE_BRIGHTNESS,
Stepper.UP,
StepperDir.UP,
),
),
Light.HOLD_BRIGHTNESS_DOWN: (
self.hold,
(
LightController.ATTRIBUTE_BRIGHTNESS,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.HOLD_BRIGHTNESS_TOGGLE: (
self.hold,
(
LightController.ATTRIBUTE_BRIGHTNESS,
Stepper.TOGGLE,
StepperDir.TOGGLE,
),
),
Light.HOLD_WHITE_VALUE_UP: (
self.hold,
(
LightController.ATTRIBUTE_WHITE_VALUE,
Stepper.UP,
StepperDir.UP,
),
),
Light.HOLD_WHITE_VALUE_DOWN: (
self.hold,
(
LightController.ATTRIBUTE_WHITE_VALUE,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.HOLD_WHITE_VALUE_TOGGLE: (
self.hold,
(
LightController.ATTRIBUTE_WHITE_VALUE,
Stepper.TOGGLE,
StepperDir.TOGGLE,
),
),
Light.HOLD_COLOR_UP: (
self.hold,
(
LightController.ATTRIBUTE_COLOR,
Stepper.UP,
StepperDir.UP,
),
),
Light.HOLD_COLOR_DOWN: (
self.hold,
(
LightController.ATTRIBUTE_COLOR,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.HOLD_COLOR_TOGGLE: (
self.hold,
(
LightController.ATTRIBUTE_COLOR,
Stepper.TOGGLE,
StepperDir.TOGGLE,
),
),
Light.HOLD_COLOR_TEMP_UP: (
self.hold,
(
LightController.ATTRIBUTE_COLOR_TEMP,
Stepper.UP,
StepperDir.UP,
),
),
Light.HOLD_COLOR_TEMP_DOWN: (
self.hold,
(
LightController.ATTRIBUTE_COLOR_TEMP,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.HOLD_COLOR_TEMP_TOGGLE: (
self.hold,
(
LightController.ATTRIBUTE_COLOR_TEMP,
Stepper.TOGGLE,
StepperDir.TOGGLE,
),
),
Light.HOLD_XY_COLOR_UP: (
self.hold,
(
LightController.ATTRIBUTE_XY_COLOR,
Stepper.UP,
StepperDir.UP,
),
),
Light.HOLD_XY_COLOR_DOWN: (
self.hold,
(
LightController.ATTRIBUTE_XY_COLOR,
Stepper.DOWN,
StepperDir.DOWN,
),
),
Light.HOLD_XY_COLOR_TOGGLE: (
self.hold,
(
LightController.ATTRIBUTE_XY_COLOR,
Stepper.TOGGLE,
StepperDir.TOGGLE,
),
),
Light.XYCOLOR_FROM_CONTROLLER: self.xycolor_from_controller,
Expand Down Expand Up @@ -626,7 +626,7 @@ def check_smooth_power_on(
self, attribute: str, direction: str, light_state: str
) -> bool:
return (
direction != Stepper.DOWN
direction != StepperDir.DOWN
and attribute == self.ATTRIBUTE_BRIGHTNESS
and self.smooth_power_on
and light_state == "off"
Expand Down Expand Up @@ -664,16 +664,18 @@ async def click(
self,
attribute: str,
direction: str,
mode: str = "stop",
mode: str = StepperMode.STOP,
steps: Optional[Number] = None,
) -> None:
attribute = self.get_option(
attribute, LightController.ATTRIBUTES_LIST, "`click` action"
)
direction = self.get_option(
direction, [Stepper.UP, Stepper.DOWN], "`click` action"
direction, [StepperDir.UP, StepperDir.DOWN], "`click` action"
)
mode = self.get_option(
mode, [StepperMode.STOP, StepperMode.LOOP], "`click` action"
)
mode = self.get_option(mode, ["stop", "loop"], "`click` action")
attribute = await self.get_attribute(attribute)
self.value_attribute = await self.get_value_attribute(attribute)
await self.change_light_state(
Expand All @@ -689,24 +691,30 @@ async def hold( # type: ignore
self,
attribute: str,
direction: str,
mode: str = "stop",
mode: str = StepperMode.STOP,
steps: Optional[Number] = None,
) -> None:
attribute = self.get_option(
attribute, LightController.ATTRIBUTES_LIST, "`hold` action"
)
direction = self.get_option(
direction, [Stepper.UP, Stepper.DOWN, Stepper.TOGGLE], "`hold` action"
direction,
[StepperDir.UP, StepperDir.DOWN, StepperDir.TOGGLE],
"`hold` action",
)
mode = self.get_option(
mode,
[StepperMode.STOP, StepperMode.LOOP, StepperMode.BOUNCE],
"`hold` action",
)
mode = self.get_option(mode, ["stop", "loop", "bounce"], "`hold` action")
attribute = await self.get_attribute(attribute)
self.value_attribute = await self.get_value_attribute(attribute)
self.log(
f"Attribute value before running the hold action: {self.value_attribute}",
level="DEBUG",
)
stepper = self.get_stepper(attribute, steps or self.automatic_steps, mode)
if direction == Stepper.TOGGLE:
if direction == StepperDir.TOGGLE:
self.log(
f"Previous direction: {stepper.previous_direction}",
level="DEBUG",
Expand Down
Loading

0 comments on commit 8d3ade6

Please sign in to comment.