So instead of trying to extend the set switch app, I just created a new one just for resetting the alarm. Mainly to reduce the complexity while I was learning and in case I ran into issues. Here’s what I ended up with:
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
# file - file in which to save 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, "plugin_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_alarm, self.args["delay"])
def appd_event(self, event_name, data, kwargs):
self.log_notify("AppDaemon restart detected")
self.run_in(self.set_alarm, 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_alarm(self, kwargs):
self.log_notify("Checking for alarm_control_panel")
# 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)
The bit that fixed the error above was changing
self.listen_event(self.ha_event, "ha_started")
to
self.listen_event(self.ha_event, "plugin_started")
as that changed in recent versions of appdaemon.
I also learnt that you shouldn’t create an empty file for it to save into - you need to just supply it with the name you want and let it create the file.
Have tested it a few times and seems to work fine