Support for lights in entity helper

This commit is contained in:
Thomas Lovén 2018-12-27 21:03:13 +01:00
parent c76ccadfdd
commit e3ba6be486
2 changed files with 26 additions and 3 deletions

View File

@ -46,7 +46,11 @@ class Entities(Base):
self.e = {} self.e = {}
def register_entity(self, name, entity): 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: class Entity:
def __init__(self, hass, entity): def __init__(self, hass, entity):
@ -90,3 +94,20 @@ class Entities(Base):
d = self.attributes d = self.attributes
d[key] = value d[key] = value
self.hass.set_state(self.entity, attributes=d) 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)

View File

@ -17,7 +17,9 @@ class HelloWorld(base.Entities, base.Timers):
def after_time(self, kwargs): def after_time(self, kwargs):
self.log("Running function") 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): def after_time2(self, kwargs):
self.log("Running function2") self.log("Running function2")
self.e['taklampa']['icon'] = "mdi:lightbulb" self.e['taklampa']['state'] = "on"
self.log(f"State is {self.e['taklampa']['state']}")