314ma

Saver

Jun 9th, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import asyncio
  2. import logging
  3.  
  4. import homeassistant.helpers.config_validation as cv
  5. from homeassistant.core import callback
  6.  
  7. from homeassistant.helpers.entity import Entity
  8. from homeassistant.helpers.service import call_from_config
  9. from homeassistant.helpers.script import Script
  10. from homeassistant.helpers.entity_component import EntityComponent
  11. from homeassistant.const import CONF_ENTITY_ID
  12.  
  13. import voluptuous as vol
  14.  
  15. _LOGGER = logging.getLogger(__name__)
  16.  
  17. DOMAIN = 'saver'
  18.  
  19. SERVICE_SAVE_ATTRIBUTE = 'save_state'
  20. SERVICE_SAVE_ATTRIBUTE_SCHEMA = vol.Schema({
  21.     vol.Required(CONF_ENTITY_ID): cv.entity_id
  22. })
  23.  
  24. SERVICE_RESTORE_ATTRIBUTE = 'restore_state'
  25. SERVICE_RESTORE_ATTRIBUTE_SCHEMA = vol.Schema({
  26.     vol.Required(CONF_ENTITY_ID): cv.entity_id
  27. })
  28.  
  29.  
  30. @asyncio.coroutine
  31. def async_setup(hass, config):
  32.     component = EntityComponent(_LOGGER, DOMAIN, hass)
  33.     saver_entity = SaverEntity(config[DOMAIN])
  34.     yield from component.async_add_entities([saver_entity])
  35.  
  36.     @callback
  37.     def save_state(call):
  38.         data = call.data
  39.         entity_id = data[CONF_ENTITY_ID]
  40.         saver_entity.save(entity_id)
  41.  
  42.     @callback
  43.     def restore_state(call):
  44.         data = call.data
  45.         entity_id = data[CONF_ENTITY_ID]
  46.         saver_entity.restore(entity_id)
  47.  
  48.     hass.services.async_register(DOMAIN, SERVICE_SAVE_ATTRIBUTE, save_state, SERVICE_SAVE_ATTRIBUTE_SCHEMA)
  49.     hass.services.async_register(DOMAIN, SERVICE_RESTORE_ATTRIBUTE, restore_state, SERVICE_RESTORE_ATTRIBUTE_SCHEMA)
  50.  
  51.     return True
  52.  
  53.  
  54. class SaverEntity(Entity):
  55.  
  56.     def __init__(self, hass):
  57.         self._hass = hass
  58.         self._db = {}
  59.  
  60.     @property
  61.     def name(self):
  62.         return DOMAIN
  63.  
  64.     def save(self, entity_id):
  65.         self._db[entity_id] = self.hass.states.get(entity_id)
  66.         print()
  67.         print("SAVING:")
  68.         print(f"   entity_id:    {entity_id}")
  69.         print(f"   db:           {self._db}")
  70.         print(f"   hass:         {self._hass}")
  71.         print(f"   state:        {self.hass.states.get(entity_id).state}")
  72.         print(f"   attributes:   {self.hass.states.get(entity_id).attributes}")
  73.         print()
  74.         self.schedule_update_ha_state()
  75.  
  76.     def restore(self, entity_id):
  77.         print()
  78.         print(f"RESTORING:")
  79.         print(f"  CURRENT:")
  80.         print(f"   entity_id:    {entity_id}")
  81.         print(f"   db:           {self._db}")
  82.         print(f"   hass:         {self._hass}")
  83.         print(f"   state:        {self.hass.states.get(entity_id).state}")
  84.         print(f"   attributes:   {self.hass.states.get(entity_id).attributes}")
  85.         old = self._db[entity_id]
  86.         print(f"  OLD:")
  87.         print(f"   state:        {old.state}")
  88.         print(f"   attributes:   {old.attributes}")
  89.         print()
  90.         # call_from_config(self.hass, {"service": "homeassistant.toggle", "data": {"entity_id": entity_id}})
  91.         script = Script(
  92.             self.hass,
  93.             [{"service": "homeassistant.toggle", "data": {"entity_id": entity_id}}]
  94.         )
  95.         print("STARTING SCRIPT")
  96.         script.async_run()
  97.         print("FINISHED SCRIPT")
  98.         # self.schedule_update_ha_state()
  99.  
  100.     @property
  101.     def state_attributes(self):
  102.         return {"entities": self._db}
  103.  
  104.     @property
  105.     def state(self):
  106.         return len(self._db)
Add Comment
Please, Sign In to add comment