callback work
This commit is contained in:
67
motion.py
67
motion.py
@@ -1,4 +1,5 @@
|
||||
from datetime import timedelta
|
||||
import re
|
||||
|
||||
from appdaemon.entity import Entity
|
||||
from appdaemon.plugins.hass.hassapi import Hass
|
||||
@@ -25,8 +26,17 @@ class Motion(Hass):
|
||||
self.listen_state(self.callback_light_on, self.args['entity'], new='on')
|
||||
self.listen_state(self.callback_light_off, self.args['entity'], new='off')
|
||||
|
||||
self.listen_motion_on()
|
||||
self.listen_motion_off(self.off_duration)
|
||||
self.sync_state()
|
||||
|
||||
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.app.entity_state:
|
||||
self.callback_light_on()
|
||||
else:
|
||||
self.callback_light_off()
|
||||
|
||||
def listen_motion_on(self):
|
||||
"""Sets up the motion on callback to activate the room
|
||||
@@ -67,38 +77,33 @@ class Motion(Hass):
|
||||
self.cancel_motion_callback(new='off')
|
||||
self.listen_motion_on()
|
||||
|
||||
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.sensor_state:
|
||||
self.callback_light_on()
|
||||
else:
|
||||
self.callback_light_off()
|
||||
|
||||
def get_app_callbacks(self, name: str = None):
|
||||
"""Gets all the callbacks associated with the app
|
||||
"""
|
||||
name = name or self.name
|
||||
for app_name, callbacks in self.get_callback_entries().items():
|
||||
if app_name == name:
|
||||
return callbacks
|
||||
|
||||
def get_motion_callback(self):
|
||||
app_callbacks = self.get_app_callbacks()
|
||||
if app_callbacks is not None:
|
||||
return {
|
||||
handle: info
|
||||
for handle, info in app_callbacks.items()
|
||||
if info['entity'] == self.sensor
|
||||
}
|
||||
else:
|
||||
return {}
|
||||
callbacks = {
|
||||
handle: info
|
||||
for app_name, callbacks in self.get_callback_entries().items()
|
||||
for handle, info in callbacks.items()
|
||||
if app_name == name
|
||||
}
|
||||
return callbacks
|
||||
|
||||
# def get_sensor_callbacks(self):
|
||||
# return {
|
||||
# handle: info
|
||||
# for handle, info in self.get_app_callbacks().items()
|
||||
# if info['entity'] == self.sensor
|
||||
# }
|
||||
|
||||
def cancel_motion_callback(self, new: str):
|
||||
for handle, info in self.get_motion_callback().items():
|
||||
if f'new={new}' in info['kwargs']:
|
||||
self.log(f'Cancelling callback for {info}')
|
||||
self.cancel_listen_state(handle)
|
||||
|
||||
callbacks = self.get_app_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:
|
||||
if new_match.group("new") == new and entity == self.sensor.entity_id:
|
||||
self.cancel_listen_state(handle)
|
||||
self.log(f'cancelled: {self.friendly_name(entity)}: {new}')
|
||||
|
||||
Reference in New Issue
Block a user