From 9121d1ef0443ee1ad74108a8e33dca9082fe50cb Mon Sep 17 00:00:00 2001 From: John Lancaster <32917998+jsl12@users.noreply.github.com> Date: Wed, 16 Oct 2024 03:57:40 +0000 Subject: [PATCH] added entity callback cancellation --- appdaemon/entity.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 appdaemon/entity.py diff --git a/appdaemon/entity.py b/appdaemon/entity.py new file mode 100644 index 0000000..246e38f --- /dev/null +++ b/appdaemon/entity.py @@ -0,0 +1,26 @@ +from appdaemon import utils + + +class Entity: + @utils.sync_decorator + async def get_callbacks(self, limit_to_app: str = None) -> dict[str, dict[str, Any]]: + async with self.AD.callbacks.callbacks_lock: + return { + handle: { + 'app_name': cb_info['name'], + 'function': cb_info['function'].__name__, + **cb_info["kwargs"] + } + for app_name, app_callbacks in self.AD.callbacks.callbacks.items() + if limit_to_app is None or app_name == limit_to_app + for handle, cb_info in app_callbacks.items() + if self.namespace == cb_info['namespace'] + and cb_info["type"] == "state" + and cb_info.get('entity') == self.entity_id + } + + @utils.sync_decorator + async def cancel_callbacks(self, all: bool = False): + for handle, info in (await self.get_callbacks()).items(): + if all or self.name == info['app_name']: + await self.AD.state.cancel_state_callback(handle, info['app_name'])