Skip to content

Commit

Permalink
fix(zha): send the whole data to the specific device for individual p…
Browse files Browse the repository at this point in the history
…arser

related to #98
  • Loading branch information
xaviml committed Jul 15, 2020
1 parent 92e851e commit 4cc0ad9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
2 changes: 1 addition & 1 deletion apps/controllerx/cx_core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def get_zha_actions_mapping(self) -> Optional[TypeActionsMapping]:
"""
return None

def get_zha_action(self, command: str, args) -> Optional[str]:
def get_zha_action(self, data: dict) -> Optional[str]:
"""
This method can be override for controllers that do not support
the standard extraction of the actions on cx_core/integration/zha.py
Expand Down
10 changes: 5 additions & 5 deletions apps/controllerx/cx_core/integration/zha.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def listen_changes(self, controller_id: str) -> None:
self.controller, self.callback, "zha_event", device_ieee=controller_id
)

def get_action(self, command: str, args):
def get_action(self, data: dict):
command = data["command"]
args = data["args"]
if isinstance(args, dict):
args = args["args"]
args = list(map(str, args))
Expand All @@ -29,11 +31,9 @@ def get_action(self, command: str, args):
return action

async def callback(self, event_name: str, data: dict, kwargs: dict) -> None:
command = data["command"]
args = data["args"]
action = self.controller.get_zha_action(command, args)
action = self.controller.get_zha_action(data)
if action is None:
# If there is no action extracted from the controller then
# we extract with the standard function
action = self.get_action(command, args)
action = self.get_action(data)
await self.controller.handle_action(action)
9 changes: 5 additions & 4 deletions apps/controllerx/cx_devices/aqara.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def get_zha_actions_mapping(self) -> TypeActionsMapping:
"quadruple": Light.SET_HALF_BRIGHTNESS,
}

def get_zha_action(self, command: str, args) -> str:
return args["click_type"]
def get_zha_action(self, data: dict) -> str:
return data["args"]["click_type"]


class WXKG11LMLightController(LightController):
Expand Down Expand Up @@ -150,8 +150,9 @@ def get_zha_actions_mapping(self) -> TypeActionsMapping:
"rotate_right": Light.CLICK_BRIGHTNESS_UP,
}

def get_zha_action(self, command: str, args) -> str:
action = command
def get_zha_action(self, data: dict) -> str:
command = action = data["command"]
args = data.get("args", {})
if command == "flip":
action = command + str(args["flip_degrees"])
return action
Expand Down
36 changes: 18 additions & 18 deletions tests/cx_devices/aqara_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@


@pytest.mark.parametrize(
"command, args, expected_action",
"data, expected_action",
[
("shake", {}, "shake"),
("knock", {}, "knock"),
("slide", {}, "slide"),
("flip", {"flip_degrees": 90}, "flip90"),
("flip", {"flip_degrees": 180}, "flip180"),
("rotate_left", {}, "rotate_left"),
("rotate_right", {}, "rotate_right"),
({"command": "shake"}, "shake"),
({"command": "knock"}, "knock"),
({"command": "slide"}, "slide"),
({"command": "flip", "args": {"flip_degrees": 90}}, "flip90",),
({"command": "flip", "args": {"flip_degrees": 180}}, "flip180"),
({"command": "rotate_left"}, "rotate_left"),
({"command": "rotate_right"}, "rotate_right"),
],
)
def test_zha_action_MFKZQ01LMLightController(command, args, expected_action):
def test_zha_action_MFKZQ01LMLightController(data, expected_action):
sut = MFKZQ01LMLightController()
action = sut.get_zha_action(command, args)
action = sut.get_zha_action(data)
assert action == expected_action


@pytest.mark.parametrize(
"command, args, expected_action",
"data, expected_action",
[
("click", {"click_type": "single"}, "single"),
("click", {"click_type": "double"}, "double"),
("click", {"click_type": "triple"}, "triple"),
("click", {"click_type": "quadruple"}, "quadruple"),
("click", {"click_type": "furious"}, "furious"),
({"command": "click", "args": {"click_type": "single"}}, "single"),
({"command": "click", "args": {"click_type": "double"}}, "double"),
({"command": "click", "args": {"click_type": "triple"}}, "triple"),
({"command": "click", "args": {"click_type": "quadruple"}}, "quadruple"),
({"command": "click", "args": {"click_type": "furious"}}, "furious"),
],
)
def test_zha_action_WXKG01LMLightController(command, args, expected_action):
def test_zha_action_WXKG01LMLightController(data, expected_action):
sut = WXKG01LMLightController()
action = sut.get_zha_action(command, args)
action = sut.get_zha_action(data)
assert action == expected_action

0 comments on commit 4cc0ad9

Please sign in to comment.