My switch_reset app stopped working correctly

I’m on 74.2 and Appdeamon 1.3.0 and somewhere (not sure during which upgrade cycle) my switch_reset app stopped working correctly. Now getting below error in Appdeamon log.

Any ideas what is causing this change in behavior and how I can solve it? p.s. I’m not a writer of apps… I think I use this one from @ReneTode

Error in Appdeamen log:

  File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 1575, in init_object
    init()
  File "/config/appdaemon/apps/switch_reset.py", line 24, in initialize
    self.device_db = shelve.open(self.args["file"])
  File "/usr/lib/python3.6/shelve.py", line 243, in open
    return DbfilenameShelf(filename, flag, protocol, writeback)
  File "/usr/lib/python3.6/shelve.py", line 227, in __init__
    Shelf.__init__(self, dbm.open(filename, flag), protocol, writeback)
  File "/usr/lib/python3.6/dbm/__init__.py", line 94, in open
    return mod.open(file, flag, mode)

The config:

Switch Reset:
  module: switch_reset
  class: SwitchReset
  #file: /home/hass/.homeassistant/switches
  file: switchesnw
  delay: 30
  log: 1

The app:

import appdaemon.plugins.hass.hassapi as hass
import shelve
import time

#
# App to reset input_boolean, input_select, input_slider, device_tracker to previous values after HA restart
#
# Minimote can send up to 8 scenes. Odd numbered scenes are short presses of the buttons, even are long presses
#
# Args:
#
#delay - amount of time after restart to set the switches
# 
#
# Release Notes
#
# Version 1.0:
#   Initial Version

class SwitchReset(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, "input_boolean")
    self.listen_state(self.state_change, "input_select")
    self.listen_state(self.state_change, "input_slider")
       
  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 == "input_boolean" or type == "input_select" or type == "input_slider":
        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 "log" in self.args:
      self.log(message)
    if "notify" in self.args:
      self.notify(message)

It seems not be seeing your db file.

is switchesnw in the same director as the app?

Regards

Well, it was not. But also if I move it to the dir the app is in, I get the same error. p.s if I remember correctly the file would also be created if it would not exist when the app starts (?)

Oh ok. WHat you need to do, is to open it when you need to. What you can do is:

def initialize(self):
    with shelve.open(self.args["file"]) as db:
        <do stuff>

Each time you need to access the database

Regards

@tyfoon do you have other things in your logfile? maybe an unautorized error?
its not an app from me.

but @Odianosen25 is right.
in this app the db gets opened in the initialise and is kept open all the time.
and there is no terminate so it also stays opened when the app is reinitialsised and that can compromise the integrity from the db.
so it should be rewritten.

is it possible this already showed up during an old upgrade from AD?

Thanks. This made me search again on all the options. It seems this is now default behavior of HASS. https://www.home-assistant.io/components/recorder/#restore-state

This works great as long as you do not (!) use an initial state. Just tested it.