From e3ba6be486fdf90e2fc529e8c96775433021ff10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Thu, 27 Dec 2018 21:03:13 +0100 Subject: [PATCH] Support for lights in entity helper --- appdaemon/apps/base.py | 23 ++++++++++++++++++++++- appdaemon/apps/hello.py | 6 ++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/appdaemon/apps/base.py b/appdaemon/apps/base.py index 3ae9dfd..e01a1dd 100644 --- a/appdaemon/apps/base.py +++ b/appdaemon/apps/base.py @@ -46,7 +46,11 @@ class Entities(Base): self.e = {} def register_entity(self, name, entity): - self.e[name] = Entities.Entity(self, entity) + domain, _ = entity.split('.') + controller = { + 'light': Entities.LightEntity, + }.get(domain, Entities.Entity) + self.e[name] = controller(self, entity) class Entity: def __init__(self, hass, entity): @@ -90,3 +94,20 @@ class Entities(Base): d = self.attributes d[key] = value self.hass.set_state(self.entity, attributes=d) + + class LightEntity(Entities.Entity): + def __init__(self, hass, entity): + hass.log("Adding a light") + super().__init__(hass, entity) + @property + def state(self): + return super().state + @state.setter + def state(self, state): + if state == 'on': + self.hass.call_service('light/turn_on', entity_id = self.entity) + elif state == 'off': + self.hass.call_service('light/turn_off', entity_id = self.entity) + else: + return + super(Entities.LightEntity, self.__class__).state.fset(self, state) diff --git a/appdaemon/apps/hello.py b/appdaemon/apps/hello.py index 3778ce9..646b227 100644 --- a/appdaemon/apps/hello.py +++ b/appdaemon/apps/hello.py @@ -17,7 +17,9 @@ class HelloWorld(base.Entities, base.Timers): def after_time(self, kwargs): self.log("Running function") - self.e['taklampa']['icon'] = "mdi:lamp" + self.e['taklampa']['state'] = "off" + self.log(f"State is {self.e['taklampa']['state']}") def after_time2(self, kwargs): self.log("Running function2") - self.e['taklampa']['icon'] = "mdi:lightbulb" + self.e['taklampa']['state'] = "on" + self.log(f"State is {self.e['taklampa']['state']}")