Skip to content

Commit

Permalink
feat(z2m): add mqtt integration
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Jun 6, 2020
1 parent e9a1ba7 commit 013f3d5
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 10 deletions.
7 changes: 4 additions & 3 deletions apps/controllerx/core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from functools import wraps
from typing import Any, Callable, DefaultDict, Dict, List, Optional, Sequence, Union

import appdaemon.plugins.hass.hassapi as hass # type: ignore
from appdaemon.plugins.hass.hassapi import Hass # type: ignore
from appdaemon.plugins.mqtt.mqttapi import Mqtt # type: ignore

import version
from const import ActionFunction, TypeActionsMapping
Expand All @@ -15,7 +16,7 @@
DEFAULT_ACTION_DELTA = 300 # In milliseconds


class Controller(hass.Hass, abc.ABC):
class Controller(Hass, Mqtt, abc.ABC):
"""
This is the parent Controller, all controllers must extend from this class.
"""
Expand Down Expand Up @@ -127,7 +128,7 @@ async def call_service(self, service: str, **attributes) -> None:
self.log(
f" - {attribute}: {value}", level="INFO", ascii_encode=False,
)
return await super().call_service(service, **attributes)
return await Hass.call_service(self, service, **attributes)

async def handle_action(self, action_key: str) -> None:
if action_key in self.actions_mapping:
Expand Down
6 changes: 5 additions & 1 deletion apps/controllerx/core/integration/deconz.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Optional

from appdaemon.plugins.hass.hassapi import Hass # type:ignore

from core.integration import Integration, TypeActionsMapping


Expand All @@ -11,7 +13,9 @@ def get_actions_mapping(self) -> Optional[TypeActionsMapping]:
return self.controller.get_deconz_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
self.controller.listen_event(self.callback, "deconz_event", id=controller_id)
Hass.listen_event(
self.controller, self.callback, "deconz_event", id=controller_id
)

async def callback(self, event_name: str, data: dict, kwargs: dict) -> None:
type_ = self.kwargs.get("type", "event")
Expand Down
4 changes: 3 additions & 1 deletion apps/controllerx/core/integration/state.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Optional

from appdaemon.plugins.hass.hassapi import Hass # type: ignore

from const import TypeActionsMapping
from core.integration import Integration

Expand All @@ -12,7 +14,7 @@ def get_actions_mapping(self) -> Optional[TypeActionsMapping]:
return self.controller.get_z2m_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
self.controller.listen_state(self.callback, controller_id)
Hass.listen_state(self.controller, self.callback, controller_id)

async def callback(
self, entity: Optional[str], attribute: Optional[str], old, new, kwargs
Expand Down
42 changes: 40 additions & 2 deletions apps/controllerx/core/integration/z2m.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
from core.integration.state import StateIntegration
from typing import Optional

from appdaemon.plugins.hass.hassapi import Hass # type: ignore
from appdaemon.plugins.mqtt.mqttapi import Mqtt # type: ignore

class Z2MIntegration(StateIntegration):
from const import TypeActionsMapping
from core.integration import Integration

STATE_HA = "ha"
STATE_MQTT = "mqtt"


class Z2MIntegration(Integration):
def get_name(self) -> str:
return "z2m"

def get_actions_mapping(self) -> Optional[TypeActionsMapping]:
return self.controller.get_z2m_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
state = self.kwargs.get("state", "ha")
if state == STATE_HA:
Hass.listen_state(self.controller, self.state_callback, controller_id)
elif state == STATE_MQTT:
Mqtt.listen_event(
self.controller,
self.event_callback,
"MQTT_MESSAGE",
topic=controller_id,
)
else:
self.controller.log(
f"Option `{state}` does not exists for `state` attribute. "
'Options are: ["ha", "mqtt"]',
level="WARNING",
)

async def event_callback(self, event_name: str, data: dict, kwargs: dict) -> None:
pass

async def state_callback(
self, entity: Optional[str], attribute: Optional[str], old, new, kwargs
) -> None:
await self.controller.handle_action(new)
6 changes: 4 additions & 2 deletions apps/controllerx/core/integration/zha.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Optional

from appdaemon.plugins.hass.hassapi import Hass # type: ignore

from const import TypeActionsMapping
from core.integration import Integration

Expand All @@ -12,8 +14,8 @@ def get_actions_mapping(self) -> Optional[TypeActionsMapping]:
return self.controller.get_zha_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
self.controller.listen_event(
self.callback, "zha_event", device_ieee=controller_id
Hass.listen_event(
self.controller, self.callback, "zha_event", device_ieee=controller_id
)

async def callback(self, event_name: str, data: dict, kwargs: dict) -> None:
Expand Down
24 changes: 24 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class A:
def log(self, message):
print(message)


class B(A):
def foo(self):
return "foo from B"


class C(A):
def foo(self):
return "foo from C"


class D(B, C):
def bar(self):
print(B.foo(self))
print(C.foo(self))
self.log("testtt")


d = D()
print(d.foo())
2 changes: 1 addition & 1 deletion tests/core/controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,4 @@ async def test_call_service(sut, mocker, service, attributes):
await sut.call_service(service, **attributes)

# Checker
call_service_stub.assert_called_once_with(service, **attributes)
call_service_stub.assert_called_once_with(sut, service, **attributes)

0 comments on commit 013f3d5

Please sign in to comment.