Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions apps/notifier/alexa_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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??
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand All @@ -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:
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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 >>>
Expand Down
16 changes: 8 additions & 8 deletions apps/notifier/gh_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ("(?<!\d),(?!\d)", ", "),
("[\n\*]", " "),
(" +", " "),
(r"[\n\*]", r" "),
(r" +", r" "),
]

CONF_MEDIA_PLAYER = "media_player"
Expand Down
4 changes: 2 additions & 2 deletions apps/notifier/helpermodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def replace_language(s: str)->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)
14 changes: 7 additions & 7 deletions apps/notifier/notification_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 = "<b>[{} - {}] {}</b>".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:
Expand Down Expand Up @@ -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 != "":
Expand Down
10 changes: 6 additions & 4 deletions apps/notifier/notifier_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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={})
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion apps/notifier/phone_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

__NOTIFY__ = "notify/"
SUB_TTS = [("[\*\-\[\]_\(\)\{\~\|\}\s]+", " ")]
SUB_TTS = [(r"[\*\-\[\]_\(\)\{\~\|\}\s]+", r" ")]


class Phone_Manager(hass.Hass):
Expand Down