upgrade notificaiton manager to use LoggerInterface
This commit is contained in:
1 parent
94754e6285
commit
f5a586c379
1 file changed
+11
-14
@@ -4,6 +4,7 @@ import ad_toolbox.smartcondition as SmartCondition
|
||||
import re
|
||||
import uuid
|
||||
from ad_toolbox.expressionparser import ParsingException
|
||||
from ad_toolbox.logger_interface import LoggerInterface
|
||||
|
||||
class Notification:
|
||||
ENTITY_PLACEHOLDER_PATTERN = re.compile(r"\{([^{}]+)\}")
|
||||
@@ -18,7 +19,7 @@ class Notification:
|
||||
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)
|
||||
except ParsingException as e:
|
||||
self.log_error(str(e))
|
||||
self.ad_api.log_error(str(e))
|
||||
continue
|
||||
|
||||
def on_conditon_fail(self, recipient):
|
||||
@@ -42,9 +43,9 @@ class Notification:
|
||||
data = { 'tag' : 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)
|
||||
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]
|
||||
if isinstance(recipient_yaml['services'], str):
|
||||
@@ -65,11 +66,11 @@ class Notification:
|
||||
try:
|
||||
state = self.ad_api.get_state(entity_id)
|
||||
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)
|
||||
|
||||
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 str(state)
|
||||
@@ -96,9 +97,9 @@ class Notification:
|
||||
self.ad_api.run_in(self.on_timeout_expired,timeout,tag = data['tag'],recipient = recipient)
|
||||
|
||||
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)
|
||||
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]
|
||||
if isinstance(recipient_yaml['services'], str):
|
||||
@@ -107,15 +108,11 @@ class Notification:
|
||||
for service in recipient_yaml['services']:
|
||||
send_notification(service)
|
||||
|
||||
def log_error(self,message,stop_app = False):
|
||||
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):
|
||||
class NotificationsManager(hass.Hass, LoggerInterface):
|
||||
|
||||
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()
|
||||
|
||||
if "notifications" in self.args:
|
||||
|
||||
Reference in new issue
Block a user