Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import logging
- import homeassistant.helpers.config_validation as cv
- from homeassistant.core import callback
- from homeassistant.helpers.entity import Entity
- from homeassistant.helpers.service import call_from_config
- from homeassistant.helpers.script import Script
- from homeassistant.helpers.entity_component import EntityComponent
- from homeassistant.const import CONF_ENTITY_ID
- import voluptuous as vol
- _LOGGER = logging.getLogger(__name__)
- DOMAIN = 'saver'
- SERVICE_SAVE_ATTRIBUTE = 'save_state'
- SERVICE_SAVE_ATTRIBUTE_SCHEMA = vol.Schema({
- vol.Required(CONF_ENTITY_ID): cv.entity_id
- })
- SERVICE_RESTORE_ATTRIBUTE = 'restore_state'
- SERVICE_RESTORE_ATTRIBUTE_SCHEMA = vol.Schema({
- vol.Required(CONF_ENTITY_ID): cv.entity_id
- })
- @asyncio.coroutine
- def async_setup(hass, config):
- component = EntityComponent(_LOGGER, DOMAIN, hass)
- saver_entity = SaverEntity(config[DOMAIN])
- yield from component.async_add_entities([saver_entity])
- @callback
- def save_state(call):
- data = call.data
- entity_id = data[CONF_ENTITY_ID]
- saver_entity.save(entity_id)
- @callback
- def restore_state(call):
- data = call.data
- entity_id = data[CONF_ENTITY_ID]
- saver_entity.restore(entity_id)
- hass.services.async_register(DOMAIN, SERVICE_SAVE_ATTRIBUTE, save_state, SERVICE_SAVE_ATTRIBUTE_SCHEMA)
- hass.services.async_register(DOMAIN, SERVICE_RESTORE_ATTRIBUTE, restore_state, SERVICE_RESTORE_ATTRIBUTE_SCHEMA)
- return True
- class SaverEntity(Entity):
- def __init__(self, hass):
- self._hass = hass
- self._db = {}
- @property
- def name(self):
- return DOMAIN
- def save(self, entity_id):
- self._db[entity_id] = self.hass.states.get(entity_id)
- print()
- print("SAVING:")
- print(f" entity_id: {entity_id}")
- print(f" db: {self._db}")
- print(f" hass: {self._hass}")
- print(f" state: {self.hass.states.get(entity_id).state}")
- print(f" attributes: {self.hass.states.get(entity_id).attributes}")
- print()
- self.schedule_update_ha_state()
- def restore(self, entity_id):
- print()
- print(f"RESTORING:")
- print(f" CURRENT:")
- print(f" entity_id: {entity_id}")
- print(f" db: {self._db}")
- print(f" hass: {self._hass}")
- print(f" state: {self.hass.states.get(entity_id).state}")
- print(f" attributes: {self.hass.states.get(entity_id).attributes}")
- old = self._db[entity_id]
- print(f" OLD:")
- print(f" state: {old.state}")
- print(f" attributes: {old.attributes}")
- print()
- # call_from_config(self.hass, {"service": "homeassistant.toggle", "data": {"entity_id": entity_id}})
- script = Script(
- self.hass,
- [{"service": "homeassistant.toggle", "data": {"entity_id": entity_id}}]
- )
- print("STARTING SCRIPT")
- script.async_run()
- print("FINISHED SCRIPT")
- # self.schedule_update_ha_state()
- @property
- def state_attributes(self):
- return {"entities": self._db}
- @property
- def state(self):
- return len(self._db)
Add Comment
Please, Sign In to add comment