callback work
This commit is contained in:
61
motion.py
61
motion.py
@@ -1,4 +1,5 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
import re
|
||||||
|
|
||||||
from appdaemon.entity import Entity
|
from appdaemon.entity import Entity
|
||||||
from appdaemon.plugins.hass.hassapi import Hass
|
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_on, self.args['entity'], new='on')
|
||||||
self.listen_state(self.callback_light_off, self.args['entity'], new='off')
|
self.listen_state(self.callback_light_off, self.args['entity'], new='off')
|
||||||
|
|
||||||
self.listen_motion_on()
|
self.sync_state()
|
||||||
self.listen_motion_off(self.off_duration)
|
|
||||||
|
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):
|
def listen_motion_on(self):
|
||||||
"""Sets up the motion on callback to activate the room
|
"""Sets up the motion on callback to activate the room
|
||||||
@@ -67,38 +77,33 @@ class Motion(Hass):
|
|||||||
self.cancel_motion_callback(new='off')
|
self.cancel_motion_callback(new='off')
|
||||||
self.listen_motion_on()
|
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):
|
def get_app_callbacks(self, name: str = None):
|
||||||
"""Gets all the callbacks associated with the app
|
"""Gets all the callbacks associated with the app
|
||||||
"""
|
"""
|
||||||
name = name or self.name
|
name = name or self.name
|
||||||
for app_name, callbacks in self.get_callback_entries().items():
|
callbacks = {
|
||||||
if app_name == name:
|
handle: info
|
||||||
|
for app_name, callbacks in self.get_callback_entries().items()
|
||||||
|
for handle, info in callbacks.items()
|
||||||
|
if app_name == name
|
||||||
|
}
|
||||||
return callbacks
|
return callbacks
|
||||||
|
|
||||||
def get_motion_callback(self):
|
# def get_sensor_callbacks(self):
|
||||||
app_callbacks = self.get_app_callbacks()
|
# return {
|
||||||
if app_callbacks is not None:
|
# handle: info
|
||||||
return {
|
# for handle, info in self.get_app_callbacks().items()
|
||||||
handle: info
|
# if info['entity'] == self.sensor
|
||||||
for handle, info in app_callbacks.items()
|
# }
|
||||||
if info['entity'] == self.sensor
|
|
||||||
}
|
|
||||||
else:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def cancel_motion_callback(self, new: str):
|
def cancel_motion_callback(self, new: str):
|
||||||
for handle, info in self.get_motion_callback().items():
|
callbacks = self.get_app_callbacks()
|
||||||
if f'new={new}' in info['kwargs']:
|
# self.log(f'Found {len(callbacks)}')
|
||||||
self.log(f'Cancelling callback for {info}')
|
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.cancel_listen_state(handle)
|
||||||
|
self.log(f'cancelled: {self.friendly_name(entity)}: {new}')
|
||||||
|
|||||||
@@ -20,14 +20,9 @@ class RoomController(Hass, Mqtt):
|
|||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
self.app_entities = self.gather_app_entities()
|
self.app_entities = self.gather_app_entities()
|
||||||
|
|
||||||
self.refresh_state_times()
|
self.refresh_state_times()
|
||||||
self.run_daily(callback=self.refresh_state_times, start='00:00:00')
|
self.run_daily(callback=self.refresh_state_times, start='00:00:00')
|
||||||
|
|
||||||
# sets up motion callbacks
|
|
||||||
# self.state_change_handle = self.listen_state(self.handle_state_change, self.entity)
|
|
||||||
# self.sync_state()
|
|
||||||
|
|
||||||
if (ha_button := self.args.get('ha_button')):
|
if (ha_button := self.args.get('ha_button')):
|
||||||
self.log(f'Setting up input button: {self.friendly_name(ha_button)}')
|
self.log(f'Setting up input button: {self.friendly_name(ha_button)}')
|
||||||
self.listen_state(callback=self.activate_any_on, entity_id=ha_button)
|
self.listen_state(callback=self.activate_any_on, entity_id=ha_button)
|
||||||
@@ -154,14 +149,6 @@ class RoomController(Hass, Mqtt):
|
|||||||
"""
|
"""
|
||||||
return any(self.get_state(entity) == 'on' for entity in self.app_entities)
|
return any(self.get_state(entity) == 'on' for entity in self.app_entities)
|
||||||
|
|
||||||
@property
|
|
||||||
def delay(self) -> timedelta:
|
|
||||||
try:
|
|
||||||
hours, minutes, seconds = map(int, self.args['delay'].split(':'))
|
|
||||||
return timedelta(hours=hours, minutes=minutes, seconds=seconds)
|
|
||||||
except Exception:
|
|
||||||
return timedelta()
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def sleep_bool(self) -> bool:
|
def sleep_bool(self) -> bool:
|
||||||
if (sleep_var := self.args.get('sleep')):
|
if (sleep_var := self.args.get('sleep')):
|
||||||
@@ -231,6 +218,7 @@ class RoomController(Hass, Mqtt):
|
|||||||
"""Activate if all of the entities are off
|
"""Activate if all of the entities are off
|
||||||
"""
|
"""
|
||||||
if self.all_off:
|
if self.all_off:
|
||||||
|
self.log(f'Activate all off kwargs: {kwargs}')
|
||||||
self.activate(*args, **kwargs)
|
self.activate(*args, **kwargs)
|
||||||
else:
|
else:
|
||||||
self.log(f'Skipped activating - everything is not off')
|
self.log(f'Skipped activating - everything is not off')
|
||||||
|
|||||||
Reference in New Issue
Block a user