upgrade notificaiton manager to use LoggerInterface

This commit is contained in:
Pierre committed 2026-11-10 19:13:47 +01:00
1 parent 94754e6285
commit f5a586c379
1 file changed
+11 -14
+11 -14
View File
@@ -4,6 +4,7 @@ import ad_toolbox.smartcondition as SmartCondition
import re import re
import uuid import uuid
from ad_toolbox.expressionparser import ParsingException from ad_toolbox.expressionparser import ParsingException
from ad_toolbox.logger_interface import LoggerInterface
class Notification: class Notification:
ENTITY_PLACEHOLDER_PATTERN = re.compile(r"\{([^{}]+)\}") ENTITY_PLACEHOLDER_PATTERN = re.compile(r"\{([^{}]+)\}")
@@ -18,7 +19,7 @@ class Notification:
for recipient in config["recipients"]: for recipient in config["recipients"]:
try: self.recipients[recipient] = SmartCondition.Evaluator(self.ad_api,self.config["recipients"][recipient]['conditions'],condition_name = name,on_succeed_cb = self.on_condition_succeed, on_fail_cb = self.on_conditon_fail,pass_condition_name_to_cb = recipient, trigger_callback_on_activation = False) try: self.recipients[recipient] = SmartCondition.Evaluator(self.ad_api,self.config["recipients"][recipient]['conditions'],condition_name = name,on_succeed_cb = self.on_condition_succeed, on_fail_cb = self.on_conditon_fail,pass_condition_name_to_cb = recipient, trigger_callback_on_activation = False)
except ParsingException as e: except ParsingException as e:
self.log_error(str(e)) self.ad_api.log_error(str(e))
continue continue
def on_conditon_fail(self, recipient): def on_conditon_fail(self, recipient):
@@ -42,9 +43,9 @@ class Notification:
data = { 'tag' : tag } data = { 'tag' : tag }
def clear_notification(service,tag): def clear_notification(service,tag):
self.ad_api.log(f"Clear notification {data} from {service}") self.ad_api.log_info(f"Clear notification {data} from {service}")
try: self.ad_api.call_service(service,message = message, data = data) try: self.ad_api.call_service(service,message = message, data = data)
except ad_exception.ServiceException: self.log_error(f"Invalid service: {service}") except ad_exception.ServiceException: self.ad_api.log_error(f"Invalid service: {service}")
recipient_yaml = self.config["recipients"][recipient] recipient_yaml = self.config["recipients"][recipient]
if isinstance(recipient_yaml['services'], str): if isinstance(recipient_yaml['services'], str):
@@ -65,11 +66,11 @@ class Notification:
try: try:
state = self.ad_api.get_state(entity_id) state = self.ad_api.get_state(entity_id)
except Exception as e: except Exception as e:
self.log_error(f"Failed to resolve '{entity_id}' in notification content: {e}") self.ad_api.log_error(f"Failed to resolve '{entity_id}' in notification content: {e}")
return match.group(0) return match.group(0)
if state is None: if state is None:
self.log_error(f"Unable to resolve '{entity_id}' in notification content") self.ad_api.log_error(f"Unable to resolve '{entity_id}' in notification content")
return match.group(0) return match.group(0)
return str(state) return str(state)
@@ -96,9 +97,9 @@ class Notification:
self.ad_api.run_in(self.on_timeout_expired,timeout,tag = data['tag'],recipient = recipient) self.ad_api.run_in(self.on_timeout_expired,timeout,tag = data['tag'],recipient = recipient)
def send_notification(service): def send_notification(service):
self.ad_api.log(f"Sending notification '{message}' to {service}{f' with data {data}' if data else ''}") self.ad_api.log_info(f"Sending notification '{message}' to {service}{f' with data {data}' if data else ''}")
try: self.ad_api.call_service(service,title = title,message = message, data = data) try: self.ad_api.call_service(service,title = title,message = message, data = data)
except ad_exception.ServiceException: self.log_error(f"Invalid service: {service}") except ad_exception.ServiceException: self.ad_api.log_error(f"Invalid service: {service}")
recipient_yaml = self.config["recipients"][recipient] recipient_yaml = self.config["recipients"][recipient]
if isinstance(recipient_yaml['services'], str): if isinstance(recipient_yaml['services'], str):
@@ -107,15 +108,11 @@ class Notification:
for service in recipient_yaml['services']: for service in recipient_yaml['services']:
send_notification(service) send_notification(service)
def log_error(self,message,stop_app = False): class NotificationsManager(hass.Hass, LoggerInterface):
self.ad_api.log(message,level = "ERROR", log = "main_log")
self.ad_api.log(message,level = "ERROR", log = "error_log")
if stop_app:
self.ad_api.stop_app(self.ad_api.name)
class NotificationsManager(hass.Hass):
def initialize(self): def initialize(self):
# Preserve the default app logger used by the previous direct log calls.
self.initialize_logger_interface(self.get_ad_api(), default_log=None)
self.notifications = dict() self.notifications = dict()
if "notifications" in self.args: if "notifications" in self.args: