Skip to content

Commit

Permalink
feat(core): add Z2M Light Controller
Browse files Browse the repository at this point in the history
related to #168
  • Loading branch information
xaviml committed Jun 5, 2022
1 parent bdbf6b4 commit 5329bb1
Show file tree
Hide file tree
Showing 27 changed files with 566 additions and 18 deletions.
1 change: 1 addition & 0 deletions apps/controllerx/controllerx.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
LightController,
MediaPlayerController,
SwitchController,
Z2MLightController,
)
from cx_devices.aqara import *
from cx_devices.aurora import *
Expand Down
23 changes: 23 additions & 0 deletions apps/controllerx/cx_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ class Light:
BRIGHTNESS_FROM_CONTROLLER_ANGLE = "brightness_from_controller_angle"


class Z2MLight:
ON = "on"
OFF = "off"
TOGGLE = "toggle"
RELEASE = "release"
ON_FULL_BRIGHTNESS = "on_full_brightness"
ON_FULL_COLOR_TEMP = "on_full_color_temp"
ON_MIN_BRIGHTNESS = "on_min_brightness"
ON_MIN_COLOR_TEMP = "on_min_color_temp"
SET_HALF_BRIGHTNESS = "set_half_brightness"
SET_HALF_COLOR_TEMP = "set_half_color_temp"
CLICK = "click"
CLICK_BRIGHTNESS_UP = "click_brightness_up"
CLICK_BRIGHTNESS_DOWN = "click_brightness_down"
CLICK_COLOR_TEMP_UP = "click_colortemp_up"
CLICK_COLOR_TEMP_DOWN = "click_colortemp_down"
HOLD = "hold"
HOLD_BRIGHTNESS_UP = "hold_brightness_up"
HOLD_BRIGHTNESS_DOWN = "hold_brightness_down"
HOLD_COLOR_TEMP_UP = "hold_colortemp_up"
HOLD_COLOR_TEMP_DOWN = "hold_colortemp_down"


class MediaPlayer:
HOLD_VOLUME_DOWN = "hold_volume_down"
HOLD_VOLUME_UP = "hold_volume_up"
Expand Down
2 changes: 2 additions & 0 deletions apps/controllerx/cx_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
from cx_core.type.light_controller import LightController
from cx_core.type.media_player_controller import MediaPlayerController
from cx_core.type.switch_controller import SwitchController
from cx_core.type.z2m_light_controller import Z2MLightController

__all__ = [
"Controller",
"ReleaseHoldController",
"LightController",
"Z2MLightController",
"MediaPlayerController",
"SwitchController",
"CoverController",
Expand Down
4 changes: 4 additions & 0 deletions apps/controllerx/cx_core/stepper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def invert_direction(direction: str) -> str:
def sign(direction: str) -> int:
return Stepper.sign_mapping[direction]

@staticmethod
def apply_sign(value: Number, direction: str) -> Number:
return Stepper.sign(direction) * value

def __init__(
self, min_max: MinMax, steps: Number, previous_direction: str = StepperDir.DOWN
) -> None:
Expand Down
3 changes: 1 addition & 2 deletions apps/controllerx/cx_core/stepper/bounce_stepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
class BounceStepper(Stepper):
def step(self, value: Number, direction: str) -> StepperOutput:
value = self.min_max.clip(value)
sign = Stepper.sign(direction)
max_ = self.min_max.max
min_ = self.min_max.min
step = (max_ - min_) / self.steps

new_value = value + sign * step
new_value = value + Stepper.apply_sign(step, direction)
if self.min_max.is_between(new_value):
return StepperOutput(round(new_value, 3), next_direction=direction)
else:
Expand Down
5 changes: 3 additions & 2 deletions apps/controllerx/cx_core/stepper/loop_stepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
class LoopStepper(Stepper):
def step(self, value: Number, direction: str) -> StepperOutput:
value = self.min_max.clip(value)
sign = Stepper.sign(direction)
# We add +1 to include `max`
max_ = self.min_max.max
min_ = self.min_max.min
step = (max_ - min_) / self.steps

new_value = (((value + step * sign) - min_) % (max_ - min_)) + min_
new_value = (
((value + Stepper.apply_sign(step, direction)) - min_) % (max_ - min_)
) + min_
new_value = round(new_value, 3)
return StepperOutput(new_value, next_direction=direction)
3 changes: 1 addition & 2 deletions apps/controllerx/cx_core/stepper/stop_stepper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ def get_direction(self, value: Number, direction: str) -> str:

def step(self, value: Number, direction: str) -> StepperOutput:
value = self.min_max.clip(value)
sign = Stepper.sign(direction)
max_ = self.min_max.max
min_ = self.min_max.min
step = (max_ - min_) / self.steps

new_value = value + sign * step
new_value = value + Stepper.apply_sign(step, direction)
new_value = round(new_value, 3)
if self.min_max.is_between(new_value):
return StepperOutput(new_value, next_direction=direction)
Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/cx_core/type/switch_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class SwitchController(TypeController[Entity]):
"""

domains = [
"switch",
"alert",
"automation",
"cover",
"input_boolean",
"light",
"media_player",
"script",
"switch",
]
entity_arg = "switch"

Expand Down
Loading

0 comments on commit 5329bb1

Please sign in to comment.