Skip to content
Snippets Groups Projects
Commit ebc1c8e0 authored by jkerdreu's avatar jkerdreu
Browse files

- Added platform handlers

- Fix Entity naming issues

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2981 b32b6428-25c9-4566-ad07-03861ab6144f
parent ef06946a
No related branches found
No related tags found
No related merge requests found
import asyncio
from homeassistant.core import HomeAssistant
from xaal.lib import AsyncEngine, tools
from xaal.lib import AsyncEngine, tools, MessageType
from xaal.schemas import devices as schemas
from xaal.monitor import Monitor, Notification
......@@ -10,6 +11,8 @@ _LOGGER = logging.getLogger(__name__)
#DB_SERVER = tools.get_uuid('d28fbc27-190f-4ee5-815a-fe05233400a2')
DB_SERVER = tools.get_uuid('9064ccbc-84ea-11e8-80cc-82ed25e6aaaa')
# DB_SERVER = tools.get_uuid('d28fbc27-190f-4ee5-815a-fe05233400a2')
def filter_msg(msg):
if msg.source == DB_SERVER:
......@@ -28,8 +31,7 @@ def filter_msg(msg):
return False
class Bridge:
class Bridge(object):
def __init__(self, hass: HomeAssistant) -> None:
"""Init dummy hub."""
......@@ -37,29 +39,41 @@ class Bridge:
self._eng = AsyncEngine()
self._dev = self.setup_device()
self._mon = Monitor(self._dev, db_server=DB_SERVER)
self._mon.subscribe (self.monitor_event)
self._eng.start()
self._eng.on_start(self.on_start)
self._eng.on_stop(self.on_stop)
self._entities = {}
self._handlers = []
@property
def engine(self):
return self._eng
def on_start(self):
async def on_start(self):
_LOGGER.warning(f"{self._eng} started")
await self.wait_is_ready()
print("Subscribing..")
self._mon.subscribe(self.monitor_event)
self._eng.subscribe(self.notification_handler)
def on_stop(self):
_LOGGER.warning(f"{self._eng} stopped")
def add_entity(self, addr, entity):
self._entities.update({addr:entity})
self._entities.update({addr: entity})
def remove_entity(self, addr):
self._entities.pop(addr)
def get_entity(self, addr):
return self._entities.get(addr, None)
def add_handler(self,handler):
print(handler)
self._handlers.append(handler)
def remove_handler(self,handler):
self._handlers.remove(handler)
def setup_device(self):
dev = schemas.hmi()
......@@ -69,29 +83,40 @@ class Bridge:
self._eng.add_device(dev)
return dev
def send_request(self, targets,action, body=None):
def send_request(self, targets, action, body=None):
self._mon.engine.send_request(self._dev, targets, action, body)
async def wait_is_ready(self) -> bool:
while 1:
if self._mon.boot_finished == True:
while 1:
if self._mon.boot_finished:
return True
await asyncio.sleep(1)
return False
def monitor_event(self,event,dev):
_LOGGER.info(f"New event {event} {dev}")
def monitor_event(self, event, dev):
entity = self.get_entity(dev.address)
if entity is None:
for h in self._handlers:
r = h.new_entity(dev)
if r:
_LOGGER.debug(f"New entity for {dev.address}")
return
_LOGGER.warning(f"Unable to find handler for {dev.address}")
return
entity = self._entities.get(dev.address, None)
#if entity == None or entity.hass is None:
# return
if entity == None:
if event in [Notification.attribute_change, Notification.metadata_change]:
_LOGGER.debug(f"{event} {entity}")
entity.async_write_ha_state()
return
# if event == Notification.new_device:
if event in [Notification.attribute_change]:
entity.async_schedule_update_ha_state()
def notification_handler(self, msg):
# right now the monitor doesn't send event on notification, so the bridge deals w/
# both monitor events & messages.
if (not msg.is_notify()) or msg.is_alive() or msg.is_attributes_change():
return
entity = self.get_entity(msg.source)
if entity and hasattr(entity, 'handle_notification'):
msg.dump()
entity.handle_notification(msg)
# FIXME: monitor_event isn't a coroutine.
# if event in [Notification.description_change, Notification.metadata_change]:
# await entity.async_device_update()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment