Files
ad-nix/apps/scene_detect.py
2024-08-27 00:42:19 -05:00

51 lines
1.7 KiB
Python

import asyncio
from appdaemon.plugins.hass.hassapi import Hass
from room_control import RoomController
class SceneDetector(Hass):
def initialize(self):
self.scene_entity = (
self.args['scene']
if self.args['scene'].startswith('scene.')
else f'scene.{self.args["scene"]}'
)
self.scene_entity = self.get_entity(self.scene_entity)
self.listen_event(
self.event_callback, event='call_service', domain='scene', service='turn_on'
)
self.log(f"Waiting for scene '{self.scene_entity.friendly_name}' to activate")
async def event_callback(self, event_name, data, cb_args):
entity_id = data['service_data']['entity_id']
if entity_id == self.scene_entity.entity_id:
await self.scene_detected()
async def scene_detected(self):
self.log(f'Detected scene activation: {self.scene_entity.friendly_name}')
await asyncio.sleep(1.0)
class MotionCanceller(SceneDetector):
@property
def room(self) -> str:
return self.args['room']
async def scene_detected(self):
await super().scene_detected()
callbacks = (await self.get_callback_entries())[self.room]
app: RoomController = await self.get_app(self.room)
for handle, info in callbacks.items():
if info['entity'] == app.motion.sensor_entity_id and 'new=off' in info['kwargs']:
self.cancel_listen_state(handle)
await self.AD.state.cancel_state_callback(handle, app.name)
self.log(f'Cancelled motion callback for {app.name}')
# self.log(json.dumps(info, indent=4))
break
else:
self.log('Did not cancel anything', level='WARNING')