Created
August 12, 2017 21:24
-
-
Save kirichkov/0c3f95bafb045bfc422c33fee6ce9793 to your computer and use it in GitHub Desktop.
Preserving Manual Alarm state in HASS through AppDaemon v1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is to be added after the standard AppDaemon configuration | |
[switch_reset] | |
module = switch_reset | |
class = SwitchReset | |
# Choose where to store the states across restarts | |
file = /home/hass/preserved_states | |
delay = 0 | |
# Change to whatever name you've given the alarm panel, you can experiment adding extra entities and see whether they work. I've tested successfully with switches and generic_thermostat | |
additional_entities = alarm_control_panel.alarm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Default location for this file is ~/.homeassistant/apps/switch_reset.py | |
import appdaemon.appapi as appapi | |
import shelve | |
import time | |
import pdb | |
# | |
# App to reset input_boolean, input_select, input_slider, device_tracker to previous values after HA restart | |
# | |
# Args: | |
# | |
#delay - amount of time after restart to set the switches | |
# | |
# | |
# Release Notes | |
# | |
# Version 1.0: | |
# Initial Version | |
class SwitchReset(appapi.AppDaemon): | |
def initialize(self): | |
self.listen_event(self.ha_event, "ha_started") | |
self.listen_event(self.appd_event, "appd_started") | |
self.listen_state(self.state_change, "input_boolean") | |
self.listen_state(self.state_change, "input_select") | |
self.listen_state(self.state_change, "input_slider") | |
if "additional_entities" in self.args: | |
self.additional_entities = [x.strip() for x in self.args["additional_entities"].split(",")] | |
for entity in self.additional_entities: | |
self.listen_state(self.state_change, entity) | |
def ha_event(self, event_name, data, kwargs): | |
self.log_notify("Home Assistant restart detected") | |
self.run_in(self.set_switches, self.args["delay"]) | |
def appd_event(self, event_name, data, kwargs): | |
self.log_notify("AppDaemon restart detected") | |
self.run_in(self.set_switches, self.args["delay"]) | |
def state_change(self, entity, attribute, old, new, kwargs): | |
dev_db = shelve.open(self.args["file"]) | |
type, id = entity.split(".") | |
if type == "climate": | |
climate_dict = { 'temperature': self.get_state(entity, 'temperature'), 'operation_mode': self.get_state(entity, 'operation_mode') } | |
if dev_db[entity] != climate_dict: | |
dev_db[entity] = climate_dict | |
self.log_notify("State change: {} to {}".format(entity, climate_dict)) | |
else: | |
dev_db[entity] = new | |
self.log_notify("State change: {} to {}".format(entity, new)) | |
dev_db.close() | |
def set_switches(self, kwargs): | |
dev_db = shelve.open(self.args["file"]) | |
self.log_notify("Setting switches") | |
# Find out what devices are avaiable. | |
# If we don't know about it initialize, if we do set the switch appropriately | |
state = self.get_state() | |
for entity in state: | |
type, id = entity.split(".") | |
if type == "input_boolean" or type == "input_select" or type == "input_slider" or (entity in self.additional_entities): | |
if entity in dev_db: | |
if type != "climate": | |
if dev_db[entity] != state[entity]["state"]: | |
self.log_notify("Setting {} to {} (was {})".format(entity, dev_db[entity], state[entity]["state"])) | |
new_state = self.set_state(entity, state = dev_db[entity]) | |
else: | |
temp = self.get_state(entity, "temperature") | |
mode = self.get_state(entity, "operation_mode") | |
if dev_db[entity]["temperature"] != temp: | |
self.log_notify("Setting {} to {} (was {})".format(entity, dev_db[entity], temp)) | |
new_state = self.call_service("climate/set_temperature", temperature = dev_db[entity]["temperature"], entity_id = entity) | |
if dev_db[entity]["operation_mode"] != mode: | |
self.log_notify("Setting {} to {} (was {})".format(entity, dev_db[entity], temp)) | |
new_state = self.call_service("climate/set_operation_mode", operation_mode = dev_db[entity]["operation_mode"], entity_id = entity) | |
else: | |
self.log_notify("Adding {}, setting value to current state ({})".format(entity, state[entity]["state"])) | |
if type != "climate": | |
dev_db[entity] = state[entity]["state"] | |
else: | |
dev_db[entity] = self.get_state(entity, "temperature") | |
dev_db.close() | |
def log_notify(self, message, level = "INFO"): | |
if "log" in self.args: | |
self.log(message) | |
if "notify" in self.args: | |
self.notify(message, "ios", name="ios") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment