from appdaemon.adapi import ADAPI class Leaving(ADAPI): def initialize(self): self.listen_state(self.handle_state_change, entity_id=self.args['person']) def handle_state_change(self, entity, attribute, old, new, kwargs): self.log(f'Changed state {old} -> {new}') if old == 'home' and new != 'home': self.turn_everything_off() def turn_everything_off(self): for app_name in self.args['apps']: try: self.get_app(app_name).deactivate(cause='leaving') except Exception as e: self.log(f'{type(e).__name__}: {e}') continue