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}')
|
||||
|
||||
@@ -20,14 +20,9 @@ class RoomController(Hass, Mqtt):
|
||||
|
||||
def initialize(self):
|
||||
self.app_entities = self.gather_app_entities()
|
||||
|
||||
self.refresh_state_times()
|
||||
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')):
|
||||
self.log(f'Setting up input button: {self.friendly_name(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)
|
||||
|
||||
@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
|
||||
def sleep_bool(self) -> bool:
|
||||
if (sleep_var := self.args.get('sleep')):
|
||||
@@ -231,6 +218,7 @@ class RoomController(Hass, Mqtt):
|
||||
"""Activate if all of the entities are off
|
||||
"""
|
||||
if self.all_off:
|
||||
self.log(f'Activate all off kwargs: {kwargs}')
|
||||
self.activate(*args, **kwargs)
|
||||
else:
|
||||
self.log(f'Skipped activating - everything is not off')
|
||||
|
||||
Reference in New Issue
Block a user