diff --git a/appdaemon/apps/entities.py b/appdaemon/apps/entities.py new file mode 100644 index 0000000..4185276 --- /dev/null +++ b/appdaemon/apps/entities.py @@ -0,0 +1,107 @@ +import base + +class Entities(base.Base): + def initialize(self): + if(getattr(super(), 'initialize', False)): + super().initialize() + + self.e = {} + + def register_entity(self, name, entity, managed=False, default=None, attributes=None): + domain, _ = entity.split('.') + controller = { + 'light': LightEntity, + 'switch': SwitchEntity, + }.get(domain, Entity) + self.e[name] = controller(self, entity, managed, default, attributes) + +class Entity: + def __init__(self, hass, entity, managed=False, state=None, attributes=None): + self._hass = hass + self._entity = entity + self._managed = managed + self._state = state + self._laststate = None + self._attributes = attributes + self._listeners = [] + + hass.listen_state(self._listener, entity=entity, attribute='all') + + if managed: + self.push() + hass.listen_event(self._service_listener, event = 'call_service') + else: + self.pull() + + def listen(self, callback, kwarg=None): + self._listeners.append({ + 'callback': callback, + 'kwarg': kwarg, + }) + return self._listeners[-1] + def unlisten(self, handle): + if handle in self._listeners: + self._listeners.remove(handle) + def _listener(self, entity, attribute, old, new, kwargs): + self._state = new['state'] + self._attributes = new['attributes'] + old, new = self._laststate, self._state + if old != new: + self._laststate = new + self._callback(old, new) + def _callback(self, old, new): + for l in self._listeners: + l['callback'](old, new, l['kwarg']) + pass + + + + def pull(self): + d = self._hass.get_state(self._entity, attribute='all') + self._state = d['state'] + self._laststate = self._state + self._attributes = d['attributes'] + + def push(self): + self._hass.set_state(self._entity, state=self._state, attributes=self._attributes) + if self._state != self._laststate: + old = self._laststate + new = self._state + self._laststate = new + self.set_state(old, new) + self._callback(old, new) + + def set_state(self, old, new): + pass + + def _service_listener(self, event, data, kwarg): + if data['service_data'].get('entity_id', '') == self._entity: + self.service_callback(data) + def service_callback(self, data): + pass + + @property + def state(self): + return self._state + @state.setter + def state(self, value): + self._state = value + + @property + def attr(self): + return self._attributes + +class LightEntity(Entity): + def set_state(self, old, new): + if new == "on": + self._hass.call_service("light/turn_on", entity_id = self._entity) + elif new == "off": + self._hass.call_service("light/turn_off", entity_id = self._entity) + +class SwitchEntity(Entity): + def service_callback(self, data): + if data['service'] == 'turn_on': + self._state = "on" + if data['service'] == 'turn_off': + self._state = "off" + self.push() diff --git a/appdaemon/apps/hello.py b/appdaemon/apps/hello.py index 844997d..246b205 100644 --- a/appdaemon/apps/hello.py +++ b/appdaemon/apps/hello.py @@ -1,6 +1,7 @@ import base +import entities -class HelloWorld(base.Entities, base.Timers): +class HelloWorld(entities.Entities, base.Timers): def initialize(self): super().initialize() @@ -9,18 +10,35 @@ class HelloWorld(base.Entities, base.Timers): self.log("You are now ready to run Apps!") self.run_in('test1', self.after_time, 3) - self.run_in('test2', self.after_time2, 5) + self.run_in('test2', self.after_time2, 7) # self.run_in(self.after_time2, 7) # self.cancel_timer('test2') self.register_entity('taklampa', 'light.kontoret') + self.e['taklampa'].listen(self.light_changes) + + self.register_entity('dark', 'switch.tod2', True, "on", { + 'icon': 'mdi:lightbulb', + 'friendly_name': 'TEST'}) + self.e['dark'].listen(self.switch) + + def switch(self, old, new, kwarg): + self.log(f"Switch switched {self.e['dark'].state}") + + def light_changes(self, old, new, kwarg): + self.log("Light changed!") + self.log(f"State is {self.e['taklampa'].state}") def after_time(self, kwargs): self.log("Running function") - self.e['taklampa'].icon = "mdi:lamp" + self.e['taklampa'].attr['icon'] = "mdi:lamp" + # self.e['taklampa'].state = "off" + self.e['taklampa'].push() self.log(f"State is {self.e['taklampa'].state}") def after_time2(self, kwargs): self.log("Running function2") - del self.e['taklampa'].icon + self.e['taklampa'].attr.pop('icon') + # self.e['taklampa'].state = "on" + self.e['taklampa'].push() self.log(f"State is {self.e['taklampa'].state}") diff --git a/appdaemon/apps/helloworld.yaml b/appdaemon/apps/helloworld.yaml index 8b1aa49..b965f31 100644 --- a/appdaemon/apps/helloworld.yaml +++ b/appdaemon/apps/helloworld.yaml @@ -1,9 +1,11 @@ global_modules: - base + - entities hello_world: global_dependencies: - base + - entities module: hello class: HelloWorld