This commit is contained in:
John Lancaster
2024-10-16 03:56:07 +00:00
parent 5af940f077
commit 5e458aca41
3 changed files with 9 additions and 10 deletions

View File

@@ -1,11 +1,8 @@
import asyncio import asyncio
from cgitb import handler
from copy import deepcopy from copy import deepcopy
from logging import Logger from logging import Logger
from typing import TYPE_CHECKING, Any, Literal from typing import TYPE_CHECKING, Any, Literal
import appdaemon.utils as utils
if TYPE_CHECKING: if TYPE_CHECKING:
from appdaemon.appdaemon import AppDaemon from appdaemon.appdaemon import AppDaemon
@@ -46,16 +43,18 @@ class Callbacks:
async def cancel_callback(self, handle: str, name: str, silent: bool = False): async def cancel_callback(self, handle: str, name: str, silent: bool = False):
async with self.callbacks_lock: async with self.callbacks_lock:
if (callbacks := self.callbacks.get(name)) \ if (callbacks := self.callbacks.get(name)) \
and (callback := callbacks.pop(handle, False)): and (callback := callbacks.pop(handle, False)):
await self.AD.state.remove_entity("admin", f"{callback['type']}_callback.{handle}") await self.AD.state.remove_entity("admin", f"{callback['type']}_callback.{handle}")
return True return True
elif not silent: elif not silent:
self.logger.warning(f"Invalid callback handle '{handle}' in cancel_callback()") self.logger.warning(f"Invalid callback handle '{
handle}' in cancel_callback()")
async def cancel_all_callbacks(self, name: str, silent: bool = False): async def cancel_all_callbacks(self, name: str, silent: bool = False):
async with self.callbacks_lock: async with self.callbacks_lock:
if callbacks := self.callbacks.pop(name, False): if callbacks := self.callbacks.pop(name, False):
self.logger.debug("Clearing %s callbacks for %s", len(callbacks), name) self.logger.debug(
"Clearing %s callbacks for %s", len(callbacks), name)
for handle, cb_info in callbacks.items(): for handle, cb_info in callbacks.items():
cb_type: Literal["event", "state", "log"] = cb_info['type'] cb_type: Literal["event", "state", "log"] = cb_info['type']
await self.AD.state.remove_entity("admin", f"{cb_type}_callback.{handle}") await self.AD.state.remove_entity("admin", f"{cb_type}_callback.{handle}")