I have a pretty simple module that performs a notify. When the notify function is defined within the same module, everything works as expected. I would like to move the notify functions (and other future reusable functions) into a separate module, but I am missing something. All files are in the same folder, so I do not think it is a path problem. I am not sure if I need to use get_app
or if there is some other issue, but any help is appreciated. Thank you.
apps.yaml
ad_utils:
module: ad_utils
global: true
monitor_ha_host:
module: monitor_ha_host
class: ha_host_alert_if_on_battery
dependencies: ad_utils
ha_host_plug_eid: sensor.the_ha_host_plug_power_meter
wattage_threshold: 2
notify_message: "The power usage on the host plug is lower than expected. Make sure the HA host is connected to power and not running off battery."
notify_title: "HA host power status"
monitor_ha_host.py:
import appdaemon.plugins.hass.hassapi as hass
import ad_utils
class ha_host_alert_if_on_battery(hass.Hass):
def initialize(self):
self.hdlTimer = None
self.listen_state(self.alert_if_ha_host_running_on_battery, self.args["ha_host_plug_eid"])
def alert_if_ha_host_running_on_battery(self, entity, attribute, old, new, **kwargs):
if float(new) < self.args["wattage_threshold"]:
# Note: The 10 second delay below is hard-coded to speed up debugging until I can get this working.
self.hdlTimer = self.run_in(ad_utils.notify_hugh_phone, 10, message = self.args["notify_message"], title = self.args["notify_title"])
else:
if self.hdlTimer != None:
self.cancel_timer(self.hdlTimer)
self.hdlTimer = None
# This works when defined here
# def notify_hugh_phone(self, **kwargs):
# self.notify(kwargs['message'], title = kwargs['title'], name = "mobile_app_hugh_martin_s_iphone")
# self.notify(kwargs.get('message'), title = kwargs.get('title'), name = "mobile_app_hugh_martin_s_iphone")
ad_utils.py:
import appdaemon.plugins.hass.hassapi as hass
def notify_hugh_phone(**kwargs):
self.notify(kwargs['message'], title = kwargs['title'], name = "mobile_app_hugh_martin_s_iphone")
return
The error is:
2024-08-16 12:46:02.536969 WARNING monitor_ha_host: ------------------------------------------------------------
2024-08-16 12:46:02.538059 WARNING monitor_ha_host: Unexpected error in worker for App monitor_ha_host:
2024-08-16 12:46:02.538297 WARNING monitor_ha_host: Worker Ags: {'id': '5496919bb7e54f7682968cf8472a33e9', 'name': 'monitor_ha_host', 'objectid': 'e2c2bae509e043eca06e3171b7927cb9', 'type': 'scheduler', 'function': <function notify_hugh_phone at 0x7fad44f34720>, 'pin_app': True, 'pin_thread': 2, 'kwargs': {'message': 'The power usage on the host plug is lower than expected. Make sure the HA host is connected to power and not running off battery.', 'title': 'HA host power status', '__thread_id': 'thread-2'}}
2024-08-16 12:46:02.538492 WARNING monitor_ha_host: ------------------------------------------------------------
2024-08-16 12:46:02.539356 WARNING monitor_ha_host: Traceback (most recent call last):
File "/usr/lib/python3.11/site-packages/appdaemon/threading.py", line 1020, in worker
funcref(**self.AD.sched.sanitize_timer_kwargs(app, args["kwargs"]))
File "/config/apps/ad_utils.py", line 8, in notify_hugh_phone
hass.Hass.notify(kwargs.get('message'), title = kwargs.get('title'), name = "mobile_app_hugh_martin_s_iphone")
File "/usr/lib/python3.11/site-packages/appdaemon/utils.py", line 231, in inner_sync_wrapper
f = run_coroutine_threadsafe(self, coro(self, *args, **kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/site-packages/appdaemon/utils.py", line 310, in run_coroutine_threadsafe
if self.AD.loop.is_running():
^^^^^^^
AttributeError: 'str' object has no attribute 'AD'