So I’m trying to set my alarm state on HASS restart, and I’ve modified the switch_reset app to just look at the alarm panel.
This is my code:
import appdaemon.plugins.hass.hassapi as hass
import shelve
import time
#
# App to reset alarm_control_panel to previous state after HA restart
#
# Args:
#
# delay - amount of time after restart to set the state
#
#
# Release Notes
#
# Version 1.0:
# Initial Version
class AlarmReset(hass.Hass):
def initialize(self):
self.device_db = shelve.open(self.args["file"])
self.listen_event(self.ha_event, "ha_started")
self.listen_event(self.appd_event, "appd_started")
self.listen_state(self.state_change, "alarm_control_panel")
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):
self.log_notify("State change: {} to {}".format(entity, new))
self.device_db[entity] = new
def set_switches(self, kwargs):
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 == "alarm_control_panel":
if entity in self.device_db:
if self.device_db[entity] != state[entity]["state"]:
self.log_notify("Setting {} to {} (was {})".format(entity, self.device_db[entity], state[entity]["state"]))
new_state = self.set_state(entity, state = self.device_db[entity])
else:
self.log_notify("Adding {}, setting value to current state ({})".format(entity, state[entity]["state"]))
self.device_db[entity] = state[entity]["state"]
def log_notify(self, message, level = "INFO"):
#if "verbose_log" in self.args:
self.log(message)
and this is my apps config:
alarm_reset:
module: set_alarm
class: AlarmReset
file: /conf/apps/preserved_states.db
log: '1'
delay: 0
it works to the point were it is saving the state into the preserved_states.db file. But when I restart my HASS container it simply restarts the app and doesn’t do anything. It doesn’t seem to see the HASS restart event as thi log line isn’t firing:
self.log_notify("Home Assistant restart detected")
2018-03-22 23:56:08.730730 INFO AppDaemon: Initializing app alarm_reset using class AlarmReset from module set_alarm
2018-03-22 23:56:26.325019 INFO alarm_reset: State change: alarm_control_panel.ha_alarm to pending
2018-03-22 23:56:46.773951 INFO alarm_reset: State change: alarm_control_panel.ha_alarm to armed_home
2018-03-22 23:56:59.670491 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:04.672444 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:09.674792 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:14.676545 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:19.679023 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:24.681233 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:29.682907 WARNING AppDaemon: HASS: Disconnected from Home Assistant, retrying in 5 seconds
2018-03-22 23:57:34.688442 INFO AppDaemon: HASS: Connected to Home Assistant 0.65.5
2018-03-22 23:57:35.552707 INFO AppDaemon: Processing restart for HASS
2018-03-22 23:57:35.552909 INFO AppDaemon: Terminating hello_world
2018-03-22 23:57:35.553307 INFO AppDaemon: Terminating alarm_reset
2018-03-22 23:57:35.553770 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2018-03-22 23:57:35.554837 INFO hello_world: Hello from AppDaemon
2018-03-22 23:57:35.556067 INFO hello_world: You are now ready to run Apps!
2018-03-22 23:57:35.556197 INFO AppDaemon: Initializing app alarm_reset using class AlarmReset from module set_alarm