diff --git a/apps/notifier/alexa_manager.py b/apps/notifier/alexa_manager.py index bee08af..c1ea020 100755 --- a/apps/notifier/alexa_manager.py +++ b/apps/notifier/alexa_manager.py @@ -51,14 +51,14 @@ MOBILE_PUSH_TYPE = (PUSH, "dropin", "dropin_notification") SUB_VOICE = [ - ("[\U00010000-\U0010ffff]", ""), # strip emoji - ("[\?\.\!,]+(?=[\?\.\!,])", ""), # strip duplicate dot and comma - ("(\s+\.|\s+\.\s+|[\.])(?! )(?![^{<]*[}>])(?![^\d.]*\d)", ". "), - ("&", " and "), # escape - ("[\n\*]", " "), # remove end-of-line (Carriage Return) - (" +", " "), # remove whitespace + (r"[\U00010000-\U0010ffff]", ""), # strip emoji + (r"[\?\.\!,]+(?=[\?\.\!,])", ""), # strip duplicate dot and comma + (r"(\s+\.|\s+\.\s+|[\.])(?! )(?![^{<]*[}>])(?![^\d.]*\d)", ". "), + (r"&", " and "), # escape + (r"[\n\*]", " "), # remove end-of-line (Carriage Return) + (r" +", " "), # remove whitespace ] -SUB_TEXT = [(" +", " "), ("\s\s+", "\n")] +SUB_TEXT = [(r" +", r" "), (r"\s\s+", r"\n")] SPEECHCON_IT = ( "a ah", @@ -369,15 +369,18 @@ def speak(self, alexa: dict, skill_id: str, cfg: dict) -> None: # Push notification - Only one device is needed push = self.check_bool(alexa.get(PUSH, False)) + if (push or data_type in MOBILE_PUSH_TYPE) and message: message_push = self.remove_tags(self.replace_regular(message, SUB_TEXT)) type_ = {TYPE: PUSH} if push else {TYPE: data_type} self.call_service( NOTIFY + ALEXA_SERVICE, - data=type_, - target=media_player[0], - title=str(alexa.get(TITLE, "")), - message=message_push, + service_data={ + "target":media_player[0], + "data":type_, + "title":str(alexa.get(TITLE, "")), + "message":message_push + } ) # Media Content # TODO Restore volume?? @@ -443,7 +446,7 @@ def str2list(self, string: str) -> list: def has_numbers(self, string: str): """Check if a string contains a number.""" - numbers = re.compile("\d{2}:\d{2}|\d{4,}|\d{3,}\.\d") + numbers = re.compile(r"\d{2}:\d{2}|\d{4,}|\d{3,}\.\d") return numbers.search(string) def remove_tags(self, text: str) -> str: @@ -597,12 +600,19 @@ def volume_auto_silent(self, media_player: list, volume: float) -> None: continue self.lg(f"DIFFERENT VOLUMES: {volume_get} - DEFAULT: {volume}") if status.get("state", "") != "playing": - self.call_service(NOTIFY + ALEXA_SERVICE, data={TYPE: "tts"}, target=i, message=m) + self.call_service( + NOTIFY + ALEXA_SERVICE, + service_data={ + "target": i, + "data": {TYPE: "tts"} + }, + message=m + ) time.sleep(2) self.call_service("media_player/volume_set", entity_id=i, volume_level=volume) # Force attribute volume level in Home assistant self.set_state(i, attributes={"volume_level": volume}) - self.call_service("alexa_media/update_last_called", return_result=True) + self.call_service("alexa_media/update_last_called") def volume_get_save(self, media_player: list, volume: float, defvol: float) -> None: """Get and save the volume of each media player only if different.""" @@ -622,7 +632,7 @@ def volume_restore(self) -> None: time.sleep(1) # Force attribute volume level in Home assistant self.set_state(i, attributes={"volume_level": j}) - self.call_service("alexa_media/update_last_called", return_result=True) + self.call_service("alexa_media/update_last_called") self.lg(f"RESTORE VOL: {i} {j} [State:" f" {self.get_state(i, attribute='volume_level')}]") def volume_set(self, media_player: list, volume: float) -> None: @@ -657,6 +667,7 @@ def worker(self): self.lg(f"WORKER: {type(data)} value {data}") self.set_state(self.binary_speak, state="on", attributes={**data}) media_player = data[MEDIA_PLAYER] + if data[AUTO_VOLUMES]: self.volume_auto_silent(media_player, data[VOLUME]) raise UnboundLocalError(data[AUTO_VOLUMES]) @@ -717,9 +728,11 @@ def worker(self): # Speak >>> self.call_service( NOTIFY + data[NOTIFIER], - data=alexa_data, - target=media_player, - message=msg.strip(), + service_data={ + "message": msg.strip(), + "target": media_player, + "data": alexa_data + } ) # Actionable Notification >>> diff --git a/apps/notifier/gh_manager.py b/apps/notifier/gh_manager.py index f42619d..e941dd7 100755 --- a/apps/notifier/gh_manager.py +++ b/apps/notifier/gh_manager.py @@ -16,16 +16,16 @@ __NOTIFY__ = "notify/" __TTS__ = "tts/" -SUB_TTS = [("[\*\-\[\]_\(\)\{\~\|\}\s]+"," ")] +SUB_TTS = [(r"[\*\-\[\]_\(\)\{\~\|\}\s]+"," ")] SUB_VOICE = [ - ("[\U00010000-\U0010ffff]", ""), # strip emoji - ("[\?\.\!,]+(?=[\?\.\!,])", ""), # Exclude duplicate - ("(\s+\.|\s+\.\s+|[\.])(?! )(?![^{]*})(?![^\d.]*\d)", ". "), - ("<.*?>",""), # HTML TAG - ("&", " and "), # escape + (r"[\U00010000-\U0010ffff]", r""), # strip emoji + (r"[\?\.\!,]+(?=[\?\.\!,])", r""), # Exclude duplicate + (r"(\s+\.|\s+\.\s+|[\.])(?! )(?![^{]*})(?![^\d.]*\d)", r". "), + (r"<.*?>",r""), # HTML TAG + (r"&", r" and "), # escape # ("(?str: # """Remove all tags from a string.""" def remove_tags(text: str)->str: - regex = re.compile("<.*?>") + regex = re.compile(r"<.*?>") return re.sub(regex, "", str(text).strip()) def has_numbers(string): - numbers = re.compile("\d{4,}|\d{3,}\.\d") + numbers = re.compile(r"\d{4,}|\d{3,}\.\d") return numbers.search(string) diff --git a/apps/notifier/notification_manager.py b/apps/notifier/notification_manager.py index aabf322..ccedd31 100755 --- a/apps/notifier/notification_manager.py +++ b/apps/notifier/notification_manager.py @@ -8,10 +8,10 @@ Class Notification_Manager handles sending text to notfyng service """ __NOTIFY__ = "notify/" -SUB_NOTIFICHE_NOWRAP = [("\s+", " "), (" +", " ")] -SUB_NOTIFICHE_WRAP = [(" +", " "), ("\s\s+", "\n")] -SUB_NOTIFIER = [("\s+", "_"), ("\.", "/")] -SUB_REMOVE_SPACE = [("\s*,\s*", ",")] +SUB_NOTIFICHE_NOWRAP = [(r"\s+", r" "), (r" +", r" ")] +SUB_NOTIFICHE_WRAP = [(r" +", r" "), (r"\s\s+", r"\n")] +SUB_NOTIFIER = [(r"\s+", r"_"), (r"\.", r"/")] +SUB_REMOVE_SPACE = [(r"\s*,\s*", r",")] class Notification_Manager(hass.Hass): @@ -24,10 +24,10 @@ def initialize(self): def prepare_text(self, html, message, title, timestamp, assistant_name): if str(html).lower() in ["true", "on", "yes", "1"]: title = "[{} - {}] {}".format(assistant_name, timestamp, title) - title = h.replace_regular(title, [("\s<", "<")]) + title = h.replace_regular(title, [(r"\s<", r"<")]) else: title = "*[{} - {}] {}*".format(assistant_name, timestamp, title) - title = h.replace_regular(title, [("\s\*", "*")]) + title = h.replace_regular(title, [(r"\s\*", r"*")]) if self.get_state(self.boolean_wrap_text) == "on": message = h.replace_regular(message, SUB_NOTIFICHE_WRAP) else: @@ -109,7 +109,7 @@ def send_notify(self, data, notify_name, assistant_name: str): extra_data.update({"photo": file_data}) # self.log("[EXTRA-DATA]: {}".format(extra_data), ascii_encode = False) if str(html).lower() not in ["true", "on", "yes", "1"]: - messaggio = messaggio.replace("_", "\_") + messaggio = messaggio.replace(r"_", r"\_") else: extra_data.update({"parse_mode": "html"}) if image != "": diff --git a/apps/notifier/notifier_dispatch.py b/apps/notifier/notifier_dispatch.py index 15fa409..58987d7 100755 --- a/apps/notifier/notifier_dispatch.py +++ b/apps/notifier/notifier_dispatch.py @@ -9,6 +9,7 @@ from requests import get, HTTPError, RequestException from zipfile import ZipFile, BadZipFile from io import BytesIO +from pathlib import Path """ Centro Notifiche - Dispatch Module @@ -134,7 +135,7 @@ def initialize(self): #### FROM CONFIGURATION BLUEPRINT ### self.config = self.get_plugin_config() - self.config_dir = "/homeassistant" #self.config["config_dir"] + self.my_config_dir = "/homeassistant" self.log(f"configuration dir: {self.config_dir}") ### FROM SENSOR CONFIG sensor_config = self.get_state("sensor.notifier_config", attribute="all", default={}) @@ -261,9 +262,10 @@ def package_download(self, delay): is_beta = self.cfg.get("beta_version") if not is_download: return - ha_config_file = self.config_dir + "/configuration.yaml" - cn_path = self.config_dir + f"/{PATH_PACKAGES}/" - blueprints_path = self.config_dir + f"/{PATH_BLUEPRINTS}/" + + ha_config_file = Path(self.my_config_dir) / "configuration.yaml" + cn_path = Path(self.my_config_dir) / PATH_PACKAGES + blueprints_path = Path(self.my_config_dir) / PATH_BLUEPRINTS ################################################### branche = "beta" if is_beta else "main" url_main = URL_ZIP.format(branche) diff --git a/apps/notifier/phone_manager.py b/apps/notifier/phone_manager.py index add9f67..ebcffa4 100755 --- a/apps/notifier/phone_manager.py +++ b/apps/notifier/phone_manager.py @@ -6,7 +6,7 @@ """ __NOTIFY__ = "notify/" -SUB_TTS = [("[\*\-\[\]_\(\)\{\~\|\}\s]+", " ")] +SUB_TTS = [(r"[\*\-\[\]_\(\)\{\~\|\}\s]+", r" ")] class Phone_Manager(hass.Hass):