from appdaemon.plugins.hass.hassapi import Hass import asyncio 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 {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}') class MotionCanceller(SceneDetector): async def scene_detected(self): await super().scene_detected() await asyncio.sleep(0.5) # needed for when the scene gets turned on from everything being off because of competing callbacks app = await self.get_app(self.args['app']) await app.cancel_motion_callback(new='off')