improved scene detect
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import json
|
||||
import re
|
||||
|
||||
from appdaemon.plugins.hass.hassapi import Hass
|
||||
from room_control import Motion
|
||||
|
||||
|
||||
class SceneDetector(Hass):
|
||||
@@ -15,25 +19,27 @@ class SceneDetector(Hass):
|
||||
)
|
||||
self.log(f'Waiting for {self.scene_entity.friendly_name} to activate')
|
||||
|
||||
def event_callback(self, event_name, data, cb_args):
|
||||
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:
|
||||
self.scene_detected()
|
||||
await self.scene_detected()
|
||||
|
||||
def scene_detected(self):
|
||||
async def scene_detected(self):
|
||||
self.log(f'Detected scene activation: {self.scene_entity.friendly_name}')
|
||||
|
||||
|
||||
class MotionCanceller(SceneDetector):
|
||||
def scene_detected(self):
|
||||
super().scene_detected()
|
||||
app = self.get_app(self.args['app'])
|
||||
try:
|
||||
self.run_in(callback=lambda *args, **kwargs: app.cancel_motion_callback(), delay=0.5)
|
||||
except Exception:
|
||||
self.log(
|
||||
f'Error cancelling motion callback for {self.args["app"]}',
|
||||
level='ERROR',
|
||||
)
|
||||
async def scene_detected(self):
|
||||
await super().scene_detected()
|
||||
app: Motion = await self.get_app(self.args['app'])
|
||||
all_callbacks = await self.get_callback_entries()
|
||||
app_callbacks = all_callbacks[app.name]
|
||||
|
||||
for handle, info in app_callbacks.items():
|
||||
if info['entity'] == app.sensor.entity_id and 'new=off' in info['kwargs']:
|
||||
self.log(f'Cancelling motion callback for {app.name}')
|
||||
await self.AD.state.cancel_state_callback(handle, app.name)
|
||||
# self.log(json.dumps(info, indent=4))
|
||||
break
|
||||
else:
|
||||
self.log('Cancelled motion callback')
|
||||
self.log('Did not cancel anything', level='WARNING')
|
||||
|
||||
Reference in New Issue
Block a user