seperate rich logging system
This commit is contained in:
36
motion.py
36
motion.py
@@ -3,9 +3,9 @@ from datetime import timedelta
|
||||
|
||||
from appdaemon.entity import Entity
|
||||
from appdaemon.plugins.hass.hassapi import Hass
|
||||
from room_control import RoomController
|
||||
from console import setup_component_logging
|
||||
|
||||
from appdaemon import utils
|
||||
from room_control import RoomController
|
||||
|
||||
|
||||
class Motion(Hass):
|
||||
@@ -26,33 +26,32 @@ class Motion(Hass):
|
||||
return self.ref_entity.get_state() == 'on'
|
||||
|
||||
def initialize(self):
|
||||
setup_component_logging(self)
|
||||
self.app: RoomController = self.get_app(self.args['app'])
|
||||
self.log(f'Connected to app {self.app.name}')
|
||||
self.log(f'Connected to AD app [room]{self.app.name}[/]')
|
||||
|
||||
base_kwargs = dict(
|
||||
entity_id=self.ref_entity.entity_id,
|
||||
immediate=True, # avoids needing to sync the state
|
||||
immediate=True, # avoids needing to sync the state
|
||||
)
|
||||
# don't need to await these because they'll already get turned into a task by the utils.sync_wrapper decorator
|
||||
self.listen_state(**base_kwargs, attribute='brightness', callback=self.callback_light_on)
|
||||
self.listen_state(**base_kwargs, new='off', callback=self.callback_light_off)
|
||||
|
||||
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"""
|
||||
self.cancel_motion_callback()
|
||||
self.listen_state(
|
||||
callback=self.app.activate_all_off,
|
||||
entity_id=self.sensor.entity_id,
|
||||
new='on',
|
||||
oneshot=True,
|
||||
cause='motion on'
|
||||
cause='motion on',
|
||||
)
|
||||
self.log(f'Waiting for motion on {self.sensor.friendly_name}')
|
||||
|
||||
def listen_motion_off(self, duration: timedelta):
|
||||
"""Sets up the motion off callback to deactivate the room
|
||||
"""
|
||||
"""Sets up the motion off callback to deactivate the room"""
|
||||
self.cancel_motion_callback()
|
||||
self.listen_state(
|
||||
callback=self.app.deactivate,
|
||||
@@ -60,27 +59,24 @@ class Motion(Hass):
|
||||
new='off',
|
||||
duration=duration.total_seconds(),
|
||||
oneshot=True,
|
||||
cause='motion off'
|
||||
cause='motion off',
|
||||
)
|
||||
self.log(f'Waiting for motion to stop on {self.sensor.friendly_name} for {duration}')
|
||||
|
||||
def callback_light_on(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
|
||||
"""Called when the light turns on
|
||||
"""
|
||||
"""Called when the light turns on"""
|
||||
if new is not None:
|
||||
self.log(f'{entity} turned on')
|
||||
duration = self.app.off_duration()
|
||||
self.listen_motion_off(duration)
|
||||
|
||||
def callback_light_off(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
|
||||
"""Called when the light turns off
|
||||
"""
|
||||
"""Called when the light turns off"""
|
||||
self.log(f'{entity} turned off')
|
||||
self.listen_motion_on()
|
||||
|
||||
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
|
||||
callbacks = {
|
||||
handle: info
|
||||
@@ -89,19 +85,17 @@ class Motion(Hass):
|
||||
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.entity_id
|
||||
handle: info for handle, info in self.get_app_callbacks().items() if info['entity'] == self.sensor.entity_id
|
||||
}
|
||||
|
||||
def cancel_motion_callback(self):
|
||||
callbacks = self.get_sensor_callbacks()
|
||||
# self.log(f'Found {len(callbacks)} callbacks for {self.sensor.entity_id}')
|
||||
for handle, info in callbacks.items():
|
||||
entity = info["entity"]
|
||||
entity = info['entity']
|
||||
kwargs = info['kwargs']
|
||||
if (m := re.match('new=(?P<new>.*?)\s', kwargs)) is not None:
|
||||
new = m.group('new')
|
||||
|
||||
Reference in New Issue
Block a user