AppDaemon / appdaemon

:page_facing_up: Python Apps for Home Automation
Other
826 stars 418 forks source link

Add optional `silent` parameter to `cancel_listen_state()` #2008

Open danroc opened 2 months ago

danroc commented 2 months ago

Description

This PR adds an optional silent parameter to the cancel_listen_state() method. If silent is set to True, no warning will be issued if the handle is not found.

Closes: #2007

Manual testing

I tested this PR with the following app:

# File: conf/apps/test.py
import hassapi as hass

class Test(hass.Hass):
    def initialize(self):
        self.cancel_listen_state('dummy-handle-1', silent=True)
        self.cancel_listen_state('dummy-handle-2', silent=False)
        self.cancel_listen_state('dummy-handle-3')
# File: conf/apps/apps.yaml
---
test:
  module: test
  class: Test

It produced the following output:

2024-05-09 22:26:53.918482 INFO AppDaemon: Calling initialize() for test
2024-05-09 22:26:53.919529 WARNING AppDaemon: Invalid callback handle 'dummy-handle-2' in cancel_state_callback() from app test
2024-05-09 22:26:53.919943 WARNING AppDaemon: Invalid callback handle 'dummy-handle-3' in cancel_state_callback() from app test
2024-05-09 22:26:53.920415 INFO AppDaemon: App initialization complete

As expected, no warning is issued for the dummy-handle-1 handle.

Documentation

The documentation was automatically updated based on the docstring:

image
jsl12 commented 2 months ago

Try adding these lines somewhere in your app definition. It'll suppress the warning log lines for the event subsystem.

import logging

logging.getLogger('AppDaemon._events').setLevel('ERROR')
danroc commented 2 months ago

Thanks for the tip @jsl12. The logger should be "AppDaemon._state" in this case, but I got the idea.

Another advantage of merging this PR is that it brings cancel_listen_state on par with cancel_timer, which already has the silent parameter.