appd - Redesign of entity manager - WIP
This commit is contained in:
parent
f3776f3f2d
commit
301ea0304d
107
appdaemon/apps/entities.py
Normal file
107
appdaemon/apps/entities.py
Normal file
@ -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()
|
@ -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}")
|
||||
|
@ -1,9 +1,11 @@
|
||||
global_modules:
|
||||
- base
|
||||
- entities
|
||||
|
||||
hello_world:
|
||||
global_dependencies:
|
||||
- base
|
||||
- entities
|
||||
module: hello
|
||||
class: HelloWorld
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user