Skip to content

Commit

Permalink
feat(cover): add Cover Controller and its Custom subclass
Browse files Browse the repository at this point in the history
This also comes with the integration of E1743 as a cover controller
  • Loading branch information
xaviml committed Apr 30, 2020
1 parent 35c34d9 commit 6c76a10
Show file tree
Hide file tree
Showing 27 changed files with 256 additions and 26 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ repos:
hooks:
- id: black
name: black
entry: black apps/controllerx tests
entry: pipenv run black apps/controllerx tests
language: system
pass_filenames: false
always_run: true
- id: flake8
name: flake8
entry: flake8 apps/controllerx
entry: pipenv run flake8 apps/controllerx
language: system
pass_filenames: false
always_run: true
- id: mypy
name: mypy
entry: mypy apps/controllerx
entry: pipenv run mypy apps/controllerx
language: system
pass_filenames: false
always_run: true
- id: pytest
name: pytest
entry: pytest
entry: pipenv run pytest
language: system
pass_filenames: false
always_run: true
8 changes: 8 additions & 0 deletions apps/controllerx/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ class Switch:
ON = "on"
OFF = "off"
TOGGLE = "toggle"


class Cover:
OPEN = "open"
CLOSE = "close"
STOP = "stop"
TOGGLE_OPEN = "toggle_open"
TOGGLE_CLOSE = "toggle_close"
1 change: 1 addition & 0 deletions apps/controllerx/controllerx.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from core import CallServiceController
from core import CustomLightController
from core import CustomMediaPlayerController
from core import CustomSwitchController
from devices.aqara import *
from devices.ikea import *
from devices.lutron import *
Expand Down
6 changes: 6 additions & 0 deletions apps/controllerx/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from core.controller import Controller, ReleaseHoldController, action
from core.custom_controller import (
CallServiceController,
CustomCoverController,
CustomLightController,
CustomMediaPlayerController,
CustomSwitchController,
)
from core.type.cover_controller import CoverController
from core.type.light_controller import LightController
from core.type.media_player_controller import MediaPlayerController
from core.type.switch_controller import SwitchController
Expand All @@ -14,8 +17,11 @@
"LightController",
"MediaPlayerController",
"SwitchController",
"CoverController",
"CustomLightController",
"CustomMediaPlayerController",
"CustomSwitchController",
"CustomCoverController",
"CallServiceController",
"action",
]
6 changes: 6 additions & 0 deletions apps/controllerx/core/custom_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from const import TypeAction, TypeActionsMapping
from core.controller import Controller, action
from core.type.cover_controller import CoverController
from core.type.light_controller import LightController
from core.type.media_player_controller import MediaPlayerController
from core.type.switch_controller import SwitchController
Expand Down Expand Up @@ -49,6 +50,11 @@ def parse_action(self, action) -> TypeAction:
return action


class CustomCoverController(CustomController, CoverController):
def parse_action(self, action) -> TypeAction:
return action


Service = Tuple[str, Dict]
Services = List[Service]

Expand Down
53 changes: 53 additions & 0 deletions apps/controllerx/core/type/cover_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from typing import Callable
from const import Cover, TypeActionsMapping
from core.controller import TypeController, action


class CoverController(TypeController):
"""
This is the main class that controls the coveres for different devices.
Type of actions:
- Open
- Close
Parameters taken:
- controller (required): Inherited from Controller
- cover (required): cover entity name
- delay (optional): Inherited from ReleaseHoldController
"""

async def initialize(self) -> None:
self.cover = self.args["cover"]
await self.check_domain(self.cover)
await super().initialize()

def get_domain(self) -> str:
return "cover"

def get_type_actions_mapping(self) -> TypeActionsMapping:
return {
Cover.OPEN: self.open,
Cover.CLOSE: self.close,
Cover.STOP: self.stop,
Cover.TOGGLE_OPEN: (self.toggle, self.open),
Cover.TOGGLE_CLOSE: (self.toggle, self.close),
}

@action
async def open(self) -> None:
await self.call_service("cover/open_cover", entity_id=self.cover)

@action
async def close(self) -> None:
await self.call_service("cover/close_cover", entity_id=self.cover)

@action
async def stop(self) -> None:
await self.call_service("cover/stop_cover", entity_id=self.cover)

@action
async def toggle(self, action: Callable) -> None:
cover_state = await self.get_entity_state(self.cover)
if cover_state == "opening" or cover_state == "closing":
await self.stop()
else:
await action()
2 changes: 1 addition & 1 deletion apps/controllerx/core/type/light_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LightController(TypeController, ReleaseHoldController):
- xy color click and hold
If a light supports xy_color and color_temperature, then xy_color will be the
default functionality. Parameters taken:
- sensor (required): Inherited from Controller
- controller (required): Inherited from Controller
- light (required): This is either the light entity name or a dictionary as
{name: string, color_mode: auto | xy_color | color_temp}
- delay (optional): Inherited from ReleaseHoldController
Expand Down
1 change: 0 additions & 1 deletion apps/controllerx/core/type/switch_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class SwitchController(TypeController):
Parameters taken:
- sensor (required): Inherited from Controller
- switch (required): Switch entity name
- delay (optional): Inherited from ReleaseHoldController
"""

async def initialize(self) -> None:
Expand Down
44 changes: 41 additions & 3 deletions apps/controllerx/devices/ikea.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from const import Light, MediaPlayer, Switch, TypeActionsMapping
from core import LightController, MediaPlayerController, action
from core.type.switch_controller import SwitchController
from const import Cover, Light, MediaPlayer, Switch, TypeActionsMapping
from core import (
CoverController,
LightController,
MediaPlayerController,
SwitchController,
action,
)


class E1810Controller(LightController):
Expand Down Expand Up @@ -165,6 +170,39 @@ def get_zha_actions_mapping(self) -> TypeActionsMapping:
return {"on": Switch.ON, "off": Switch.OFF}


class E1743CoverController(CoverController):
# Different states reported from the controller:
# on, off

def get_z2m_actions_mapping(self) -> TypeActionsMapping:
return {
"on": Cover.TOGGLE_OPEN,
"off": Cover.TOGGLE_CLOSE,
"brightness_up": Cover.OPEN,
"brightness_down": Cover.CLOSE,
"brightness_stop": Cover.STOP,
}

def get_deconz_actions_mapping(self) -> TypeActionsMapping:
return {
1002: Cover.TOGGLE_OPEN,
2002: Cover.TOGGLE_CLOSE,
1001: Cover.OPEN,
2001: Cover.CLOSE,
1003: Cover.STOP,
2003: Cover.STOP,
}

def get_zha_actions_mapping(self) -> TypeActionsMapping:
return {
"on": Cover.TOGGLE_OPEN,
"off": Cover.TOGGLE_CLOSE,
"move_with_on_off_0_83": Cover.OPEN,
"move_1_83": Cover.CLOSE,
"stop": Cover.STOP,
}


class ICTCG1Controller(LightController):
# Different states reported from the controller:
# rotate_left, rotate_left_quick
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/E1524_E1810.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ device_support:
- "Hold < 🠖 Previous source"
- "Hold > 🠖 Next source"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "toggle 🠖 Click ⏻"
Expand Down
9 changes: 8 additions & 1 deletion docs/_data/controllers/E1743.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ device_support:
mapping:
- "Click \"I\" 🠖 Turn on"
- "Click \"O\" 🠖 Turn off"
- type: Cover
controller: E1743CoverController
mapping:
- "Click \"I\" 🠖 Open/Stop cover"
- "Click \"O\" 🠖 Close/Stop cover"
- "Hold \"I\" 🠖 Open cover"
- "Hold \"O\" 🠖 Close cover"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "on 🠖 Click \"I\""
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/E1744.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ device_support:
- "2 click 🠖 Skip forward"
- "3 click 🠖 Skip backward"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "rotate_left 🠖 Left turn"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/ICTCG1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ device_support:
- "Right turn 🠖 Brighten up"
- "Quick right turn 🠖 Full brightness"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "rotate_left 🠖 Left turn"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/IM6001-BTP01.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ device_support:
- "2 clicks 🠖 Skip forwards"
- "Hold the button 🠖 Skip backward"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "single_click 🠖 1 click"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/MFKZQ01LM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ device_support:
- "Rotate cube to left 🠖 Dim down (1 step)"
- "Rotate cube to right 🠖 Brighten up (1 step)"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "shake 🠖 Shake the cube"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/WXCJKG13LM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ device_support:
- "Double click bottom right button 🠖 Full color temp (if supported)"
- "Hold bottom right button 🠖 Color temp up / Right color wheel"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "button_1_single 🠖 Single click top left button"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/WXKG01LM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ device_support:
- "4 clicks 🠖 50% brightness"
- "Hold the button 🠖 Brightness up/down with direction changes"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "single 🠖 1 click"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/WXKG11LM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ note: >-
One ends with `_action` and the other one with `_click`.
<a href="https://github.com/xaviml/controllerx/issues/28#issuecomment-587799277"> See here.</a>
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "single 🠖 1 click"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/WXKG12LM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ note: >-
One ends with `_action` and the other one with `_click`.
<a href="https://github.com/xaviml/controllerx/issues/28#issuecomment-587799277"> See here.</a>
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "single 🠖 1 click"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/ZYCT-202.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ device_support:
- "Hold brightness down 🠖 Volume down"
- "Click \"O\" 🠖 Play/Pause"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "on 🠖 Click \"I\""
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/double-key-wireless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ device_support:
- "Double click on right 🠖 Brighten up (1 step)"
- "Long click on right 🠖 Dim down (1 step)"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "both 🠖 Click on both"
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/controllers/hue-dimmer-switch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ device_support:
- "Click \"O\" 🠖 Turn off"
- "Hold \"O\" 🠖 Color temp down / Left color wheel"
integrations:
- name: Zigbee2mqtt
- name: Zigbee2MQTT
codename: z2m
actions:
- "on-press 🠖 Click \"I\""
Expand Down
36 changes: 35 additions & 1 deletion docs/others/custom-controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,46 @@ example_app:
class: CustomSwitchController
controller: 00:67:88:56:06:78:9b:3f
integration: zha
media_player: switch.kitchen_dishwasher
switch: switch.kitchen_dishwasher
mapping:
"on": toggle
"off": toggle
```

## Custom cover controller

Class: `CustomCoverController`

This controller lets you map controller events with predefined cover actions. This is a [Cover controller](/controllerx/start/type-configuration#cover-controller), so it inheritance all its parameters. This is the list of predefined actions that can be mapped as a value in the key-value map from the `mapping` attribute.

| value | description |
| ------------ | -------------------------------------------------- |
| open | It opens the cover |
| close | It closes the cover |
| stop | It stops the cover |
| toggle_open | It stops the cover if running and opens otherwise |
| toggle_close | It stops the cover if running and closes otherwise |

#### Example of CustomCoverController

This is an example that uses the controller E1810 to control a cover with Zigbee2MQTT. The mapping from the z2m event ids can be found in [here](/controllerx/controllers/E1524_E1810) and the values are from the list under `Zigbee2MQTT`.

```yaml
example_app:
module: controllerx
class: CustomCoverController
controller: sensor.e1810_controller
integration: z2m
cover: cover.kitchen
mapping:
brightness_up_click: toggle_open
brightness_down_click: toggle_close
brightness_up_hold: open
brightness_up_release: stop
brightness_down_hold: close
brightness_down_release: stop
```

## Call service controller

Class: `CallServiceController`
Expand Down

0 comments on commit 6c76a10

Please sign in to comment.