async working...?
This commit is contained in:
73
motion.py
73
motion.py
@@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import re
|
||||
|
||||
@@ -6,6 +7,23 @@ from appdaemon.plugins.hass.hassapi import Hass
|
||||
from room_control import RoomController
|
||||
|
||||
|
||||
class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
|
||||
def get_event_loop(self):
|
||||
try:
|
||||
# Try to get the current event loop
|
||||
loop = super().get_event_loop()
|
||||
except RuntimeError as ex:
|
||||
if "There is no current event loop" in str(ex):
|
||||
# If there's no current loop, create a new one and set it
|
||||
loop = self.new_event_loop()
|
||||
self.set_event_loop(loop)
|
||||
else:
|
||||
raise
|
||||
return loop
|
||||
|
||||
# Set the custom event loop policy
|
||||
asyncio.set_event_loop_policy(CustomEventLoopPolicy())
|
||||
|
||||
class Motion(Hass):
|
||||
@property
|
||||
def sensor(self) -> Entity:
|
||||
@@ -23,34 +41,35 @@ class Motion(Hass):
|
||||
def ref_entity_state(self) -> bool:
|
||||
return self.ref_entity.get_state() == 'on'
|
||||
|
||||
@property
|
||||
def off_duration(self) -> timedelta:
|
||||
return self.app.off_duration
|
||||
|
||||
def initialize(self):
|
||||
self.app: RoomController = self.get_app(self.args['app'])
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
except RuntimeError:
|
||||
loop = asyncio.new_event_loop()
|
||||
|
||||
self.app: RoomController = loop.run_until_complete(self.get_app(self.args['app']))
|
||||
self.log(f'Connected to app {self.app.name}')
|
||||
|
||||
self.listen_state(self.callback_light_on, self.ref_entity.entity_id, new='on')
|
||||
self.listen_state(self.callback_light_off, self.ref_entity.entity_id, new='off')
|
||||
|
||||
self.sync_state()
|
||||
loop.run_until_complete(self.sync_state())
|
||||
|
||||
def sync_state(self):
|
||||
async def sync_state(self):
|
||||
"""Synchronizes the callbacks with the state of the light.
|
||||
|
||||
Essentially mimics the `state_change` callback based on the current state of the light.
|
||||
"""
|
||||
if self.ref_entity_state:
|
||||
self.callback_light_on()
|
||||
await self.callback_light_on()
|
||||
else:
|
||||
self.callback_light_off()
|
||||
await self.callback_light_off()
|
||||
|
||||
def listen_motion_on(self):
|
||||
async def listen_motion_on(self):
|
||||
"""Sets up the motion on callback to activate the room
|
||||
"""
|
||||
self.log(f'Waiting for motion on {self.sensor.friendly_name}')
|
||||
self.motion_on_handle = self.listen_state(
|
||||
self.motion_on_handle = await self.listen_state(
|
||||
callback=self.app.activate_all_off,
|
||||
entity_id=self.sensor.entity_id,
|
||||
new='on',
|
||||
@@ -58,11 +77,11 @@ class Motion(Hass):
|
||||
cause='motion on'
|
||||
)
|
||||
|
||||
def listen_motion_off(self, duration: timedelta):
|
||||
async def listen_motion_off(self, duration: timedelta):
|
||||
"""Sets up the motion off callback to deactivate the room
|
||||
"""
|
||||
self.log(f'Waiting for motion to stop on {self.sensor.friendly_name}')
|
||||
self.motion_off_handle = self.listen_state(
|
||||
self.motion_off_handle = await self.listen_state(
|
||||
callback=self.app.deactivate,
|
||||
entity_id=self.sensor.entity_id,
|
||||
new='off',
|
||||
@@ -71,46 +90,46 @@ class Motion(Hass):
|
||||
cause='motion off'
|
||||
)
|
||||
|
||||
def callback_light_on(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
|
||||
async def callback_light_on(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
|
||||
"""Called when the light turns on
|
||||
"""
|
||||
self.log('Light on callback')
|
||||
self.cancel_motion_callback(new='on')
|
||||
self.listen_motion_off(self.off_duration)
|
||||
await self.cancel_motion_callback(new='on')
|
||||
await self.listen_motion_off(await self.app.off_duration())
|
||||
|
||||
def callback_light_off(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
|
||||
async def callback_light_off(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
|
||||
"""Called when the light turns off
|
||||
"""
|
||||
self.log('Light off callback')
|
||||
self.cancel_motion_callback(new='off')
|
||||
self.listen_motion_on()
|
||||
await self.cancel_motion_callback(new='off')
|
||||
await self.listen_motion_on()
|
||||
|
||||
def get_app_callbacks(self, name: str = None):
|
||||
async def get_app_callbacks(self, name: str = None):
|
||||
"""Gets all the callbacks associated with the app
|
||||
"""
|
||||
name = name or self.name
|
||||
callbacks = {
|
||||
handle: info
|
||||
for app_name, callbacks in self.get_callback_entries().items()
|
||||
for app_name, callbacks in (await self.get_callback_entries()).items()
|
||||
for handle, info in callbacks.items()
|
||||
if app_name == name
|
||||
}
|
||||
return callbacks
|
||||
|
||||
def get_sensor_callbacks(self):
|
||||
async def get_sensor_callbacks(self):
|
||||
return {
|
||||
handle: info
|
||||
for handle, info in self.get_app_callbacks().items()
|
||||
for handle, info in (await self.get_app_callbacks()).items()
|
||||
if info['entity'] == self.sensor.entity_id
|
||||
}
|
||||
|
||||
def cancel_motion_callback(self, new: str):
|
||||
callbacks = self.get_sensor_callbacks()
|
||||
async def cancel_motion_callback(self, new: str):
|
||||
callbacks = await self.get_sensor_callbacks()
|
||||
# self.log(f'Found {len(callbacks)}')
|
||||
for handle, info in callbacks.items():
|
||||
entity = info["entity"]
|
||||
new_match = re.match('new=(?P<new>.*?)\s', info['kwargs'])
|
||||
# self.log(f'{handle}: {info["entity"]}: {info["kwargs"]}')
|
||||
if new_match is not None and new_match.group("new") == new:
|
||||
self.cancel_listen_state(handle)
|
||||
self.log(f'cancelled: {self.friendly_name(entity)}: {new}')
|
||||
await self.cancel_listen_state(handle)
|
||||
self.log(f'cancelled: {await self.friendly_name(entity)}: {new}')
|
||||
|
||||
Reference in New Issue
Block a user