diff --git a/.gitignore b/.gitignore index b9c815b..167f14f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ !/appdaemon/ /appdaemon/* !/appdaemon/apps +*.json diff --git a/appdaemon/apps/helpers/entities.py b/appdaemon/apps/helpers/entities.py index 2f58058..e82eb8c 100644 --- a/appdaemon/apps/helpers/entities.py +++ b/appdaemon/apps/helpers/entities.py @@ -1,5 +1,31 @@ +import os +import json import base +class EntityManager(base.Base): + ENTITY_FILE = 'entities.json' + + def initialize(self): + statedir = os.path.dirname(os.path.realpath(__file__)) + self.statefile = os.path.join(statedir, self.ENTITY_FILE) + + if not os.path.exists(self.statefile): + os.mknod(self.statefile) + self.db = {} + else: + with open(self.statefile) as f: + self.db = json.load(f) + self.get = self.db.get + + def terminate(self): + with open(self.statefile, 'w') as f: + json.dump(self.db, f) + + def __getitem__(self, key): + return self.db.get(key, None) + def __setitem__(self, key, value): + self.db[key] = value + class Entities(base.Base): def initialize(self): @@ -25,7 +51,8 @@ class Entity: self._hass = hass self._entity = entity self._managed = managed - self._state = state + self.manager = hass.get_app('entity_manager') if managed else {} + self._state = self.manager.get(entity, state) self._laststate = None self._attributes = attributes self._listeners = [] @@ -55,6 +82,7 @@ class Entity: self._attributes = new['attributes'] old, new = self._laststate, self._state if old != new: + self.manager[self._entity] = self._state self._laststate = new self._callback(old, new) @@ -67,6 +95,7 @@ class Entity: def pull(self): d = self._hass.get_state(self._entity, attribute='all') self._state = d['state'] + self.manager[self._entity] = self._state self._laststate = self._state self._attributes = d['attributes'] @@ -76,6 +105,7 @@ class Entity: self._laststate = self._state self._callback(self._laststate, self._state) self._hass.set_state(self._entity, state=self._state, attributes=self._attributes) + self.manager[self._entity] = self._state def set_state(self, old, new): pass diff --git a/appdaemon/apps/helpers/entity_manager.yaml b/appdaemon/apps/helpers/entity_manager.yaml new file mode 100644 index 0000000..d81800a --- /dev/null +++ b/appdaemon/apps/helpers/entity_manager.yaml @@ -0,0 +1,5 @@ +entity_manager: + module: entities + class: EntityManager + global_dependencies: + - base diff --git a/appdaemon/apps/timed_lights.yaml b/appdaemon/apps/timed_lights.yaml index c41c396..f1d2c57 100644 --- a/appdaemon/apps/timed_lights.yaml +++ b/appdaemon/apps/timed_lights.yaml @@ -5,6 +5,7 @@ outside_lights: - entities dependencies: - timeofday + - entity_manager lights: - light.ute_framsidan @@ -16,6 +17,7 @@ decorative_lights: - entities dependencies: - timeofday + - entity_manager lights: - light.deko_vardagsrum - light.skotbord