So got something interesting here. If I make a call in an app to self.get_state(‘input_select.house_mode’) I get what I would expect a return value Home, Away, etc. However if I put this same line into a util file and call the function I place it in then I get the error KeyError: ‘input_select.house_mode’. Any idea what might be happening? I can verify I am reaching the function in my util file and other utils in this file are working. Here is my code.
my app:
self.utils = self.get_app('utils')
mode = self.utils.get_house_mode()
utils file:
def get_house_mode(self):
"""
Method to get the current house mode.
"""
return self.get_state('input_select.house_mode')
So the reason for the util is to be able to only update one spot for house mode changes if I change the entity name. Keep in mind as well this is not all my code nor the only util I use in this file so the get app call was a line that would be in the file in the first place. I will also have quite a bit more logic in my util here once I finish it but just noticed that the same call in one file did not behave the same in another. Totally understand why you would question the logic here though but trust me it has a place ;).
self.sanitize_state_kwargs(app, args["kwargs"]))
File "/config/appdaemon/apps/test_routine.py", line 52, in test_routine_cb
this = self.utils.get_house_mode()
File "/config/appdaemon/apps/utils.py", line 224, in get_house_mode
return self.get_state('input_select.house_mode')
File "/usr/lib/python3.6/site-packages/appdaemon/appapi.py", line 214, in get_state
return self.AD.get_state(namespace, device, entity, attribute)
File "/usr/lib/python3.6/site-packages/appdaemon/appdaemon.py", line 696, in get_state
return deepcopy(self.state[namespace])
KeyError: 'input_select.house_mode'
Yep tried restating as a few times. From what I can tell the dependencies are correct. Correct me if im wrong but I shouldn’t be able to get to that function at all if they were not.
Looking at the stack dump, the utils app is calling the correct method, but it doesn’t appear to have the correct data. How is the app initialised? Not that I know when AD apps actually get the data from HA, but it could be that the app needs to run the initialize method to make it happen.
you can get that function even if dependencies are wrong.
those 2 things are unrelated. dependencies make sure that apps get reloaded in the right order and that the right apps get reloaded when a change is made. get_app is just there to make the connection.
but @gpbenton is right. an app needs to be able to initialise.
so every app needs this at the start:
import appdaemon.plugins.hass.hassapi as hass
class any_name(hass.Hass):
def initialize(self):
# do some stuff (or not)
return
if any part of that is missing or wrong, the app wont initialise correct.
Wow I feel like a dummy you guys were right it was in my hass import. Sorry it took so long to get back just now able to sit down at my code. Guess I never noticed the import issue before as I wasn’t calling anything directly in hass from the utils file until now. Thanks for all the help everyone! I just got into my first (owned) home here so will be doing a lot more complicated smart home integration so I Look forward to being more active on the forums here! Thanks so much again!