pydantic work

This commit is contained in:
John Lancaster
2024-04-02 22:30:23 -05:00
parent c0a22b63f8
commit 7f8d0311ab
2 changed files with 71 additions and 37 deletions

View File

@@ -1,13 +1,29 @@
import re
from datetime import timedelta
from typing import Literal, Optional
from appdaemon.entity import Entity
from appdaemon.plugins.hass.hassapi import Hass
from console import setup_component_logging
from console import console, setup_component_logging
from pydantic import BaseModel, TypeAdapter
from room_control import RoomController
class CallbackEntry(BaseModel):
entity: str
event: Optional[str] = None
type: Literal['state', 'event']
kwargs: str
function: str
name: str
pin_app: bool
pin_thread: int
Callbacks = dict[str, dict[str, CallbackEntry]]
class Motion(Hass):
@property
def sensor(self) -> Entity:
@@ -39,14 +55,33 @@ class Motion(Hass):
)
if self.sensor_state != self.ref_entity_state:
self.log(f'Sensor is {self.sensor_state} ' f'and light is {self.ref_entity_state}', level='WARNING')
self.log(
f'Sensor is {self.sensor_state} ' f'and light is {self.ref_entity_state}',
level='WARNING',
)
if self.sensor_state:
self.app.activate(kwargs={'cause': f'Syncing state with {self.sensor.entity_id}'})
# 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,
attribute='brightness',
callback=self.callback_light_on,
)
self.listen_state(**base_kwargs, new='off', callback=self.callback_light_off)
if callbacks := self.callbacks():
for handle, entry in callbacks.items():
self.log(f'Handle [yellow]{handle[:4]}[/]: {entry.function}')
def callbacks(self):
data = TypeAdapter(Callbacks).validate_python(self.get_callback_entries())
name: str = self.name
try:
return data[name]
except KeyError:
return []
def listen_motion_on(self):
"""Sets up the motion on callback to activate the room"""
self.cancel_motion_callback()
@@ -59,7 +94,9 @@ class Motion(Hass):
)
self.log(f'Waiting for motion on [friendly_name]{self.sensor.friendly_name}[/]')
if self.sensor_state:
self.log(f'[friendly_name]{self.sensor.friendly_name}[/] is already on', level='WARNING')
self.log(
f'[friendly_name]{self.sensor.friendly_name}[/] is already on', level='WARNING'
)
def listen_motion_off(self, duration: timedelta):
"""Sets up the motion off callback to deactivate the room"""
@@ -72,10 +109,14 @@ class Motion(Hass):
oneshot=True,
cause='motion off',
)
self.log(f'Waiting for motion to stop on [friendly_name]{self.sensor.friendly_name}[/] for {duration}')
self.log(
f'Waiting for [friendly_name]{self.sensor.friendly_name}[/] to be clear for {duration}'
)
if not self.sensor_state:
self.log(f'[friendly_name]{self.sensor.friendly_name}[/] is currently off', level='WARNING')
self.log(
f'[friendly_name]{self.sensor.friendly_name}[/] is currently off', level='WARNING'
)
def callback_light_on(self, entity=None, attribute=None, old=None, new=None, kwargs=None):
"""Called when the light turns on"""
@@ -102,7 +143,9 @@ class Motion(Hass):
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):