mirror of
https://github.com/joBr99/nspanel-lovelace-ui.git
synced 2025-12-21 06:54:24 +01:00
add queue for outgoing messages
This commit is contained in:
@@ -297,7 +297,7 @@ class EntitiesCard(HACard):
|
|||||||
result = f"{self.title}~{self.gen_nav()}"
|
result = f"{self.title}~{self.gen_nav()}"
|
||||||
for e in self.entities:
|
for e in self.entities:
|
||||||
result += e.render(cardType=self.type)
|
result += e.render(cardType=self.type)
|
||||||
libs.panel_cmd.entityUpd(self.panel.sendTopic, result)
|
libs.panel_cmd.entityUpd(self.panel.msg_out_queue, self.panel.sendTopic, result)
|
||||||
|
|
||||||
class QRCard(HACard):
|
class QRCard(HACard):
|
||||||
def __init__(self, locale, config, panel):
|
def __init__(self, locale, config, panel):
|
||||||
@@ -311,7 +311,7 @@ class QRCard(HACard):
|
|||||||
result = f"{self.title}~{self.gen_nav()}~{self.qrcode}"
|
result = f"{self.title}~{self.gen_nav()}~{self.qrcode}"
|
||||||
for e in self.entities:
|
for e in self.entities:
|
||||||
result += e.render()
|
result += e.render()
|
||||||
libs.panel_cmd.entityUpd(self.panel.sendTopic, result)
|
libs.panel_cmd.entityUpd(self.panel.msg_out_queue, self.panel.sendTopic, result)
|
||||||
|
|
||||||
class PowerCard(HACard):
|
class PowerCard(HACard):
|
||||||
def __init__(self, locale, config, panel):
|
def __init__(self, locale, config, panel):
|
||||||
@@ -329,7 +329,7 @@ class PowerCard(HACard):
|
|||||||
# if isinstance(speed, str):
|
# if isinstance(speed, str):
|
||||||
# speed = apis.ha_api.render_template(speed)
|
# speed = apis.ha_api.render_template(speed)
|
||||||
result += f"~{speed}"
|
result += f"~{speed}"
|
||||||
libs.panel_cmd.entityUpd(self.panel.sendTopic, result)
|
libs.panel_cmd.entityUpd(self.panel.msg_out_queue, self.panel.sendTopic, result)
|
||||||
|
|
||||||
class MediaCard(HACard):
|
class MediaCard(HACard):
|
||||||
def __init__(self, locale, config, panel):
|
def __init__(self, locale, config, panel):
|
||||||
@@ -366,7 +366,7 @@ class MediaCard(HACard):
|
|||||||
for e in self.entities[1:]:
|
for e in self.entities[1:]:
|
||||||
button_str += e.render()
|
button_str += e.render()
|
||||||
result = f"{self.title}~{self.gen_nav()}~{main_entity.entity_id}~{title}~~{author}~~{volume}~{iconplaypause}~{onoffbutton}~{shuffleBtn}{media_icon}{button_str}"
|
result = f"{self.title}~{self.gen_nav()}~{main_entity.entity_id}~{title}~~{author}~~{volume}~{iconplaypause}~{onoffbutton}~{shuffleBtn}{media_icon}{button_str}"
|
||||||
libs.panel_cmd.entityUpd(self.panel.sendTopic, result)
|
libs.panel_cmd.entityUpd(self.panel.msg_out_queue, self.panel.sendTopic, result)
|
||||||
|
|
||||||
class ClimateCard(HACard):
|
class ClimateCard(HACard):
|
||||||
def __init__(self, locale, config, panel):
|
def __init__(self, locale, config, panel):
|
||||||
@@ -461,7 +461,7 @@ class ClimateCard(HACard):
|
|||||||
detailPage = "0"
|
detailPage = "0"
|
||||||
|
|
||||||
result = f"{self.title}~{self.gen_nav()}~{main_entity.entity_id}~{current_temp} {temperature_unit}~{dest_temp}~{state_value}~{min_temp}~{max_temp}~{step_temp}{icon_res}~{currently_translation}~{state_translation}~{action_translation}~{temperature_unit_icon}~{dest_temp2}~{detailPage}"
|
result = f"{self.title}~{self.gen_nav()}~{main_entity.entity_id}~{current_temp} {temperature_unit}~{dest_temp}~{state_value}~{min_temp}~{max_temp}~{step_temp}{icon_res}~{currently_translation}~{state_translation}~{action_translation}~{temperature_unit_icon}~{dest_temp2}~{detailPage}"
|
||||||
libs.panel_cmd.entityUpd(self.panel.sendTopic, result)
|
libs.panel_cmd.entityUpd(self.panel.msg_out_queue, self.panel.sendTopic, result)
|
||||||
|
|
||||||
class AlarmCard(HACard):
|
class AlarmCard(HACard):
|
||||||
def __init__(self, locale, config, panel):
|
def __init__(self, locale, config, panel):
|
||||||
@@ -531,7 +531,7 @@ class AlarmCard(HACard):
|
|||||||
if len(supported_modes) < 4:
|
if len(supported_modes) < 4:
|
||||||
arm_buttons += "~"*((4-len(supported_modes))*2)
|
arm_buttons += "~"*((4-len(supported_modes))*2)
|
||||||
result = f"{self.title}~{self.gen_nav()}~{main_entity.entity_id}{arm_buttons}~{icon}~{color}~{numpad}~{flashing}~{add_btn}"
|
result = f"{self.title}~{self.gen_nav()}~{main_entity.entity_id}{arm_buttons}~{icon}~{color}~{numpad}~{flashing}~{add_btn}"
|
||||||
libs.panel_cmd.entityUpd(self.panel.sendTopic, result)
|
libs.panel_cmd.entityUpd(self.panel.msg_out_queue, self.panel.sendTopic, result)
|
||||||
|
|
||||||
|
|
||||||
class UnlockCard(HACard):
|
class UnlockCard(HACard):
|
||||||
@@ -555,7 +555,7 @@ class UnlockCard(HACard):
|
|||||||
numpad = "enable"
|
numpad = "enable"
|
||||||
|
|
||||||
result = f"{self.title}~{self.gen_nav()}~{entity_id}{arm_buttons}~{icon}~{color}~{numpad}~disable~"
|
result = f"{self.title}~{self.gen_nav()}~{entity_id}{arm_buttons}~{icon}~{color}~{numpad}~disable~"
|
||||||
libs.panel_cmd.entityUpd(self.panel.sendTopic, result)
|
libs.panel_cmd.entityUpd(self.panel.msg_out_queue, self.panel.sendTopic, result)
|
||||||
|
|
||||||
class Screensaver(HACard):
|
class Screensaver(HACard):
|
||||||
def __init__(self, locale, config, panel):
|
def __init__(self, locale, config, panel):
|
||||||
@@ -583,7 +583,7 @@ class Screensaver(HACard):
|
|||||||
result = ""
|
result = ""
|
||||||
for e in self.entities:
|
for e in self.entities:
|
||||||
result += e.render(cardType=self.type)
|
result += e.render(cardType=self.type)
|
||||||
libs.panel_cmd.weatherUpdate(self.panel.sendTopic, result[1:])
|
libs.panel_cmd.weatherUpdate(self.panel.msg_out_queue, self.panel.sendTopic, result[1:])
|
||||||
|
|
||||||
statusUpdateResult = ""
|
statusUpdateResult = ""
|
||||||
icon1font = ""
|
icon1font = ""
|
||||||
@@ -601,7 +601,7 @@ class Screensaver(HACard):
|
|||||||
else:
|
else:
|
||||||
statusUpdateResult += "~~"
|
statusUpdateResult += "~~"
|
||||||
|
|
||||||
libs.panel_cmd.statusUpdate(self.panel.sendTopic, f"{statusUpdateResult}~{icon1font}~{icon2font}")
|
libs.panel_cmd.statusUpdate(self.panel.msg_out_queue, self.panel.sendTopic, f"{statusUpdateResult}~{icon1font}~{icon2font}")
|
||||||
|
|
||||||
|
|
||||||
def card_factory(locale, settings, panel):
|
def card_factory(locale, settings, panel):
|
||||||
@@ -625,7 +625,7 @@ def card_factory(locale, settings, panel):
|
|||||||
return "NotImplemented", None
|
return "NotImplemented", None
|
||||||
return card.iid, card
|
return card.iid, card
|
||||||
|
|
||||||
def detail_open(locale, detail_type, ha_entity_id, entity_id, sendTopic=None, options_list=None):
|
def detail_open(locale, detail_type, ha_entity_id, entity_id, msg_out_queue, sendTopic=None, options_list=None):
|
||||||
data = libs.home_assistant.get_entity_data(ha_entity_id)
|
data = libs.home_assistant.get_entity_data(ha_entity_id)
|
||||||
if data:
|
if data:
|
||||||
state = data.get("state")
|
state = data.get("state")
|
||||||
@@ -804,8 +804,8 @@ def detail_open(locale, detail_type, ha_entity_id, entity_id, sendTopic=None, op
|
|||||||
#update timer in a second
|
#update timer in a second
|
||||||
def update_time():
|
def update_time():
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
out = detail_open(locale, detail_type, ha_entity_id, entity_id, sendTopic=sendTopic)
|
out = detail_open(locale, detail_type, ha_entity_id, entity_id, msg_out_queue, sendTopic=sendTopic)
|
||||||
libs.panel_cmd.entityUpdateDetail(sendTopic, out)
|
libs.panel_cmd.entityUpdateDetail(msg_out_queue, sendTopic, out)
|
||||||
tt = threading.Thread(target=update_time, args=())
|
tt = threading.Thread(target=update_time, args=())
|
||||||
tt.daemon = True
|
tt.daemon = True
|
||||||
tt.start()
|
tt.start()
|
||||||
|
|||||||
@@ -5,45 +5,44 @@ def init(mqtt_client_from_manager):
|
|||||||
mqtt_client = mqtt_client_from_manager
|
mqtt_client = mqtt_client_from_manager
|
||||||
|
|
||||||
|
|
||||||
def custom_send(topic, msg):
|
def custom_send(msg_out_queue, topic, msg):
|
||||||
global mqtt_client
|
msg_out_queue.put((topic, msg))
|
||||||
mqtt_client.publish(topic, msg)
|
|
||||||
logging.debug("Sent Message to NsPanel (%s): %s", topic, msg)
|
logging.debug("Sent Message to NsPanel (%s): %s", topic, msg)
|
||||||
|
|
||||||
|
|
||||||
def page_type(topic, target_page):
|
def page_type(msg_out_queue, topic, target_page):
|
||||||
if target_page == "cardUnlock":
|
if target_page == "cardUnlock":
|
||||||
target_page = "cardAlarm"
|
target_page = "cardAlarm"
|
||||||
custom_send(topic, f"pageType~{target_page}")
|
custom_send(msg_out_queue, topic, f"pageType~{target_page}")
|
||||||
|
|
||||||
|
|
||||||
def send_time(topic, time, addTimeText=""):
|
def send_time(msg_out_queue, topic, time, addTimeText=""):
|
||||||
custom_send(topic, f"time~{time}~{addTimeText}")
|
custom_send(msg_out_queue, topic, f"time~{time}~{addTimeText}")
|
||||||
|
|
||||||
|
|
||||||
def send_date(topic, date):
|
def send_date(msg_out_queue, topic, date):
|
||||||
custom_send(topic, f"date~{date}")
|
custom_send(msg_out_queue, topic, f"date~{date}")
|
||||||
|
|
||||||
|
|
||||||
def entityUpd(topic, data):
|
def entityUpd(msg_out_queue, topic, data):
|
||||||
custom_send(topic, f"entityUpd~{data}")
|
custom_send(msg_out_queue, topic, f"entityUpd~{data}")
|
||||||
|
|
||||||
def weatherUpdate(topic, data):
|
def weatherUpdate(msg_out_queue, topic, data):
|
||||||
custom_send(topic, f"weatherUpdate~{data}")
|
custom_send(msg_out_queue, topic, f"weatherUpdate~{data}")
|
||||||
|
|
||||||
def timeout(topic, timeout):
|
def timeout(msg_out_queue, topic, timeout):
|
||||||
custom_send(topic, f"timeout~{timeout}")
|
custom_send(msg_out_queue, topic, f"timeout~{timeout}")
|
||||||
|
|
||||||
def dimmode(topic, dimValue, dimValueNormal, backgroundColor, fontColor, featExperimentalSliders):
|
def dimmode(msg_out_queue, topic, dimValue, dimValueNormal, backgroundColor, fontColor, featExperimentalSliders):
|
||||||
if dimValue==dimValueNormal:
|
if dimValue==dimValueNormal:
|
||||||
dimValue=dimValue-1
|
dimValue=dimValue-1
|
||||||
custom_send(topic, f"dimmode~{dimValue}~{dimValueNormal}~{backgroundColor}~{fontColor}~{featExperimentalSliders}")
|
custom_send(msg_out_queue, topic, f"dimmode~{dimValue}~{dimValueNormal}~{backgroundColor}~{fontColor}~{featExperimentalSliders}")
|
||||||
|
|
||||||
def entityUpdateDetail(topic, data):
|
def entityUpdateDetail(msg_out_queue, topic, data):
|
||||||
custom_send(topic, f"entityUpdateDetail~{data}")
|
custom_send(msg_out_queue, topic, f"entityUpdateDetail~{data}")
|
||||||
|
|
||||||
def entityUpdateDetail2(topic, data):
|
def entityUpdateDetail2(msg_out_queue, topic, data):
|
||||||
custom_send(topic, f"entityUpdateDetail2~{data}")
|
custom_send(msg_out_queue, topic, f"entityUpdateDetail2~{data}")
|
||||||
|
|
||||||
def statusUpdate(topic, data):
|
def statusUpdate(msg_out_queue, topic, data):
|
||||||
custom_send(topic, f"statusUpdate~{data}")
|
custom_send(msg_out_queue, topic, f"statusUpdate~{data}")
|
||||||
@@ -1,224 +1,161 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import logging
|
import logging
|
||||||
import paho.mqtt.client as mqtt
|
import time
|
||||||
import time
|
import subprocess
|
||||||
import json
|
import libs.home_assistant
|
||||||
import subprocess
|
import libs.panel_cmd
|
||||||
import libs.home_assistant
|
import yaml
|
||||||
import libs.panel_cmd
|
from panel import LovelaceUIPanel
|
||||||
import yaml
|
import os
|
||||||
from uuid import getnode as get_mac
|
import threading
|
||||||
from panel import LovelaceUIPanel
|
from watchdog.events import FileSystemEventHandler
|
||||||
import os
|
from watchdog.observers import Observer
|
||||||
import threading
|
import signal
|
||||||
from watchdog.events import FileSystemEventHandler
|
import sys
|
||||||
from watchdog.observers import Observer
|
from queue import Queue
|
||||||
import signal
|
from mqtt import MqttManager
|
||||||
import sys
|
|
||||||
from queue import Queue
|
logging.getLogger("watchdog").propagate = False
|
||||||
|
|
||||||
logging.getLogger("watchdog").propagate = False
|
settings = {}
|
||||||
|
panels = {}
|
||||||
settings = {}
|
panel_in_queues = {}
|
||||||
panels = {}
|
panel_out_queue = Queue(maxsize=20)
|
||||||
panel_queues = {}
|
last_settings_file_mtime = 0
|
||||||
last_settings_file_mtime = 0
|
mqtt_connect_time = 0
|
||||||
mqtt_connect_time = 0
|
has_sent_reload_command = False
|
||||||
has_sent_reload_command = False
|
|
||||||
mqtt_client_name = "NSPanelLovelaceManager_" + str(get_mac())
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
client = mqtt.Client(mqtt_client_name)
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
def on_ha_update(entity_id):
|
||||||
|
global panel_in_queues
|
||||||
def on_connect(client, userdata, flags, rc):
|
# send HA updates to all panels
|
||||||
global settings
|
for queue in panel_in_queues.values():
|
||||||
logging.info("Connected to MQTT Server")
|
queue.put(("HA:", entity_id))
|
||||||
# subscribe to panelRecvTopic of each panel
|
|
||||||
for settings_panel in settings["nspanels"].values():
|
def connect():
|
||||||
client.subscribe(settings_panel["panelRecvTopic"])
|
global settings, panel_out_queue
|
||||||
|
if settings["mqtt_server"] != "":
|
||||||
def on_ha_update(entity_id):
|
MqttManager(settings, panel_out_queue, panel_in_queues)
|
||||||
global panel_queues
|
else:
|
||||||
for queue in panel_queues.values():
|
logging.info("MQTT values not configured, will not connect.")
|
||||||
queue.put(("HA:", entity_id))
|
|
||||||
|
# MQTT Connected, start APIs if configured
|
||||||
def on_message(client, userdata, msg):
|
if settings["home_assistant_address"] != "" and settings["home_assistant_token"] != "":
|
||||||
global panel_queues
|
libs.home_assistant.init(settings, on_ha_update)
|
||||||
try:
|
libs.home_assistant.connect()
|
||||||
if msg.payload.decode() == "":
|
else:
|
||||||
return
|
logging.info("Home Assistant values not configured, will not connect.")
|
||||||
parts = msg.topic.split('/')
|
|
||||||
if msg.topic in panel_queues.keys():
|
def setup_panels():
|
||||||
data = json.loads(msg.payload.decode('utf-8'))
|
global settings, panel_in_queues
|
||||||
if "CustomRecv" in data:
|
# Create NsPanel object
|
||||||
queue = panel_queues[msg.topic]
|
for name, settings_panel in settings["nspanels"].items():
|
||||||
queue.put(("MQTT:", data["CustomRecv"]))
|
if "timeZone" not in settings_panel:
|
||||||
else:
|
settings_panel["timeZone"] = settings.get("timeZone", "Europe/Berlin")
|
||||||
logging.debug("Received unhandled message on topic: %s", msg.topic)
|
if "locale" not in settings_panel:
|
||||||
|
settings_panel["timezone"] = settings.get("locale", "en_US")
|
||||||
except Exception: # pylint: disable=broad-exception-caught
|
if "hiddenCards" not in settings_panel:
|
||||||
logging.exception("Something went wrong during processing of message:")
|
settings_panel["hiddenCards"] = settings.get("hiddenCards", [])
|
||||||
try:
|
|
||||||
logging.error(msg.payload.decode('utf-8'))
|
msg_in_queue = Queue(maxsize=20)
|
||||||
except: # pylint: disable=bare-except
|
panel_in_queues[settings_panel["panelRecvTopic"]] = msg_in_queue
|
||||||
logging.error(
|
panel_thread = threading.Thread(target=panel_thread_target, args=(msg_in_queue, name, settings_panel, panel_out_queue))
|
||||||
"Something went wrong when processing the exception message, couldn't decode payload to utf-8.")
|
panel_thread.daemon = True
|
||||||
|
panel_thread.start()
|
||||||
def get_config_file():
|
|
||||||
CONFIG_FILE = os.getenv('CONFIG_FILE')
|
def panel_thread_target(queue_in, name, settings_panel, queue_out):
|
||||||
if not CONFIG_FILE:
|
panel = LovelaceUIPanel(name, settings_panel, queue_out)
|
||||||
CONFIG_FILE = './panels.yaml'
|
while True:
|
||||||
return CONFIG_FILE
|
msg = queue_in.get()
|
||||||
|
if msg[0] == "MQTT:":
|
||||||
def get_config(file):
|
panel.customrecv_event_callback(msg[1])
|
||||||
global settings
|
elif msg[0] == "HA:":
|
||||||
|
panel.ha_event_callback(msg[1])
|
||||||
try:
|
|
||||||
with open(file, 'r', encoding="utf8") as file:
|
def get_config_file():
|
||||||
settings = yaml.safe_load(file)
|
CONFIG_FILE = os.getenv('CONFIG_FILE')
|
||||||
except yaml.YAMLError as exc:
|
if not CONFIG_FILE:
|
||||||
print ("Error while parsing YAML file:")
|
CONFIG_FILE = './panels.yaml'
|
||||||
if hasattr(exc, 'problem_mark'):
|
return CONFIG_FILE
|
||||||
if exc.context != None:
|
|
||||||
print (' parser says\n' + str(exc.problem_mark) + '\n ' +
|
def get_config(file):
|
||||||
str(exc.problem) + ' ' + str(exc.context) +
|
global settings
|
||||||
'\nPlease correct data and retry.')
|
|
||||||
else:
|
try:
|
||||||
print (' parser says\n' + str(exc.problem_mark) + '\n ' +
|
with open(file, 'r', encoding="utf8") as file:
|
||||||
str(exc.problem) + '\nPlease correct data and retry.')
|
settings = yaml.safe_load(file)
|
||||||
else:
|
except yaml.YAMLError as exc:
|
||||||
print ("Something went wrong while parsing yaml file")
|
print ("Error while parsing YAML file:")
|
||||||
return False
|
if hasattr(exc, 'problem_mark'):
|
||||||
|
if exc.context != None:
|
||||||
if not settings.get("mqtt_username"):
|
print (' parser says\n' + str(exc.problem_mark) + '\n ' +
|
||||||
settings["mqtt_username"] = os.getenv('MQTT_USER')
|
str(exc.problem) + ' ' + str(exc.context) +
|
||||||
if not settings.get("mqtt_password"):
|
'\nPlease correct data and retry.')
|
||||||
settings["mqtt_password"] = os.getenv('MQTT_PASS')
|
else:
|
||||||
if not settings.get("mqtt_port"):
|
print (' parser says\n' + str(exc.problem_mark) + '\n ' +
|
||||||
settings["mqtt_port"] = os.getenv('MQTT_PORT')
|
str(exc.problem) + '\nPlease correct data and retry.')
|
||||||
if not settings.get("mqtt_server"):
|
else:
|
||||||
settings["mqtt_server"] = os.getenv('MQTT_SERVER')
|
print ("Something went wrong while parsing yaml file")
|
||||||
|
return False
|
||||||
|
|
||||||
settings["is_addon"] = False
|
if not settings.get("mqtt_username"):
|
||||||
|
settings["mqtt_username"] = os.getenv('MQTT_USER')
|
||||||
if not settings.get("home_assistant_token"):
|
if not settings.get("mqtt_password"):
|
||||||
st = os.getenv('SUPERVISOR_TOKEN')
|
settings["mqtt_password"] = os.getenv('MQTT_PASS')
|
||||||
if st and "home_assistant_token" not in settings and "home_assistant_address" not in settings:
|
if not settings.get("mqtt_port"):
|
||||||
settings["home_assistant_token"] = st
|
settings["mqtt_port"] = os.getenv('MQTT_PORT')
|
||||||
settings["home_assistant_address"] = "http://supervisor"
|
if not settings.get("mqtt_server"):
|
||||||
settings["is_addon"] = True
|
settings["mqtt_server"] = os.getenv('MQTT_SERVER')
|
||||||
return True
|
|
||||||
|
|
||||||
def connect():
|
settings["is_addon"] = False
|
||||||
global settings, home_assistant, client
|
|
||||||
client.on_connect = on_connect
|
if not settings.get("home_assistant_token"):
|
||||||
client.on_message = on_message
|
st = os.getenv('SUPERVISOR_TOKEN')
|
||||||
client.username_pw_set(
|
if st and "home_assistant_token" not in settings and "home_assistant_address" not in settings:
|
||||||
settings["mqtt_username"], settings["mqtt_password"])
|
settings["home_assistant_token"] = st
|
||||||
# Wait for connection
|
settings["home_assistant_address"] = "http://supervisor"
|
||||||
connection_return_code = 0
|
settings["is_addon"] = True
|
||||||
mqtt_server = settings["mqtt_server"]
|
return True
|
||||||
mqtt_port = int(settings["mqtt_port"])
|
|
||||||
logging.info("Connecting to %s:%i as %s",
|
def config_watch():
|
||||||
mqtt_server, mqtt_port, mqtt_client_name)
|
class ConfigChangeEventHandler(FileSystemEventHandler):
|
||||||
while True:
|
def __init__(self, base_paths):
|
||||||
try:
|
self.base_paths = base_paths
|
||||||
client.connect(mqtt_server, mqtt_port, 5)
|
|
||||||
break # Connection call did not raise exception, connection is sucessfull
|
def dispatch(self, event):
|
||||||
except: # pylint: disable=bare-except
|
for base_path in self.base_paths:
|
||||||
logging.exception(
|
if event.src_path.endswith(base_path):
|
||||||
"Failed to connect to MQTT %s:%i. Will try again in 10 seconds. Code: %s", mqtt_server, mqtt_port, connection_return_code)
|
super(ConfigChangeEventHandler, self).dispatch(event)
|
||||||
time.sleep(10.)
|
return
|
||||||
|
|
||||||
# MQTT Connected, start APIs if configured
|
def on_modified(self, event):
|
||||||
if settings["home_assistant_address"] != "" and settings["home_assistant_token"] != "":
|
logging.info('Modification detected. Reloading panels.')
|
||||||
libs.home_assistant.init(settings, on_ha_update)
|
pid = os.getpid()
|
||||||
libs.home_assistant.connect()
|
os.kill(pid, signal.SIGTERM)
|
||||||
else:
|
|
||||||
logging.info("Home Assistant values not configured, will not connect.")
|
logging.info('Watching for changes in config file')
|
||||||
|
project_files = []
|
||||||
libs.panel_cmd.init(client)
|
project_files.append(get_config_file())
|
||||||
|
handler = ConfigChangeEventHandler(project_files)
|
||||||
setup_panels()
|
observer = Observer()
|
||||||
|
observer.schedule(handler, path=os.path.dirname(get_config_file()), recursive=True)
|
||||||
def loop():
|
observer.start()
|
||||||
global client
|
while True:
|
||||||
# Loop MQTT
|
time.sleep(1)
|
||||||
client.loop_forever()
|
|
||||||
|
def signal_handler(signum, frame):
|
||||||
def setup_panels():
|
logging.info(f"Received signal {signum}. Initiating restart...")
|
||||||
global settings, panel_queues
|
python = sys.executable
|
||||||
# Create NsPanel object
|
os.execl(python, python, *sys.argv)
|
||||||
for name, settings_panel in settings["nspanels"].items():
|
|
||||||
if "timeZone" not in settings_panel:
|
if __name__ == '__main__':
|
||||||
settings_panel["timeZone"] = settings.get("timeZone", "Europe/Berlin")
|
signal.signal(signal.SIGTERM, signal_handler)
|
||||||
if "locale" not in settings_panel:
|
threading.Thread(target=config_watch).start()
|
||||||
settings_panel["timezone"] = settings.get("locale", "en_US")
|
if (get_config(get_config_file())):
|
||||||
if "hiddenCards" not in settings_panel:
|
connect()
|
||||||
settings_panel["hiddenCards"] = settings.get("hiddenCards", [])
|
setup_panels()
|
||||||
|
else:
|
||||||
#panels[name] = LovelaceUIPanel(name, settings_panel)
|
while True:
|
||||||
|
|
||||||
mqtt_queue = Queue(maxsize=20)
|
|
||||||
panel_queues[settings_panel["panelRecvTopic"]] = mqtt_queue
|
|
||||||
panel_thread = threading.Thread(target=panel_thread_target, args=(mqtt_queue, name, settings_panel))
|
|
||||||
panel_thread.daemon = True
|
|
||||||
|
|
||||||
panel_thread.start()
|
|
||||||
|
|
||||||
def panel_thread_target(queue, name, settings_panel):
|
|
||||||
panel = LovelaceUIPanel(name, settings_panel)
|
|
||||||
while True:
|
|
||||||
msg = queue.get()
|
|
||||||
#print(msg)
|
|
||||||
if msg[0] == "MQTT:":
|
|
||||||
panel.customrecv_event_callback(msg[1])
|
|
||||||
elif msg[0] == "HA:":
|
|
||||||
panel.ha_event_callback(msg[1])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def config_watch():
|
|
||||||
class ConfigChangeEventHandler(FileSystemEventHandler):
|
|
||||||
def __init__(self, base_paths):
|
|
||||||
self.base_paths = base_paths
|
|
||||||
|
|
||||||
def dispatch(self, event):
|
|
||||||
for base_path in self.base_paths:
|
|
||||||
if event.src_path.endswith(base_path):
|
|
||||||
super(ConfigChangeEventHandler, self).dispatch(event)
|
|
||||||
return
|
|
||||||
|
|
||||||
def on_modified(self, event):
|
|
||||||
logging.info('Modification detected. Reloading panels.')
|
|
||||||
pid = os.getpid()
|
|
||||||
os.kill(pid, signal.SIGTERM)
|
|
||||||
|
|
||||||
logging.info('Watching for changes in config file')
|
|
||||||
project_files = []
|
|
||||||
project_files.append(get_config_file())
|
|
||||||
handler = ConfigChangeEventHandler(project_files)
|
|
||||||
observer = Observer()
|
|
||||||
observer.schedule(handler, path=os.path.dirname(get_config_file()), recursive=True)
|
|
||||||
observer.start()
|
|
||||||
while True:
|
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
def signal_handler(signum, frame):
|
|
||||||
logging.info(f"Received signal {signum}. Initiating restart...")
|
|
||||||
python = sys.executable
|
|
||||||
os.execl(python, python, *sys.argv)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
signal.signal(signal.SIGTERM, signal_handler)
|
|
||||||
threading.Thread(target=config_watch).start()
|
|
||||||
if (get_config(get_config_file())):
|
|
||||||
connect()
|
|
||||||
loop()
|
|
||||||
else:
|
|
||||||
while True:
|
|
||||||
time.sleep(100)
|
time.sleep(100)
|
||||||
68
nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/mqtt.py
Normal file
68
nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/mqtt.py
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
from uuid import getnode as get_mac
|
||||||
|
import paho.mqtt.client as mqtt
|
||||||
|
import logging
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
import threading
|
||||||
|
|
||||||
|
|
||||||
|
class MqttManager:
|
||||||
|
def __init__(self, settings, msg_in_queue, msg_out_queue_list):
|
||||||
|
mqtt_client_name = "NSPanelLovelaceManager_" + str(get_mac())
|
||||||
|
self.client = mqtt.Client(mqtt_client_name)
|
||||||
|
self.msg_in_queue = msg_in_queue
|
||||||
|
self.msg_out_queue_list = msg_out_queue_list
|
||||||
|
self.settings = settings
|
||||||
|
|
||||||
|
self.client.on_connect = self.on_mqtt_connect
|
||||||
|
self.client.on_message = self.on_mqtt_message
|
||||||
|
self.client.username_pw_set(
|
||||||
|
settings["mqtt_username"], settings["mqtt_password"])
|
||||||
|
# Wait for connection
|
||||||
|
connection_return_code = 0
|
||||||
|
mqtt_server = settings["mqtt_server"]
|
||||||
|
mqtt_port = int(settings["mqtt_port"])
|
||||||
|
logging.info("Connecting to %s:%i as %s",
|
||||||
|
mqtt_server, mqtt_port, mqtt_client_name)
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
self.client.connect(mqtt_server, mqtt_port, 5)
|
||||||
|
break # Connection call did not raise exception, connection is sucessfull
|
||||||
|
except: # pylint: disable=bare-except
|
||||||
|
logging.exception(
|
||||||
|
"Failed to connect to MQTT %s:%i. Will try again in 10 seconds. Code: %s", mqtt_server, mqtt_port, connection_return_code)
|
||||||
|
time.sleep(10.)
|
||||||
|
self.client.loop_start()
|
||||||
|
process_thread = threading.Thread(target=self.process_in_queue, args=(self.client, self.msg_in_queue))
|
||||||
|
process_thread.daemon = True
|
||||||
|
process_thread.start()
|
||||||
|
|
||||||
|
def on_mqtt_connect(self, client, userdata, flags, rc):
|
||||||
|
logging.info("Connected to MQTT Server")
|
||||||
|
# subscribe to panelRecvTopic of each panel
|
||||||
|
for settings_panel in self.settings["nspanels"].values():
|
||||||
|
client.subscribe(settings_panel["panelRecvTopic"])
|
||||||
|
|
||||||
|
def on_mqtt_message(self, client, userdata, msg):
|
||||||
|
try:
|
||||||
|
if msg.payload.decode() == "":
|
||||||
|
return
|
||||||
|
if msg.topic in self.msg_out_queue_list.keys():
|
||||||
|
data = json.loads(msg.payload.decode('utf-8'))
|
||||||
|
if "CustomRecv" in data:
|
||||||
|
queue = self.msg_out_queue_list[msg.topic]
|
||||||
|
queue.put(("MQTT:", data["CustomRecv"]))
|
||||||
|
else:
|
||||||
|
logging.debug("Received unhandled message on topic: %s", msg.topic)
|
||||||
|
except Exception: # pylint: disable=broad-exception-caught
|
||||||
|
logging.exception("Something went wrong during processing of message:")
|
||||||
|
try:
|
||||||
|
logging.error(msg.payload.decode('utf-8'))
|
||||||
|
except: # pylint: disable=bare-except
|
||||||
|
logging.error(
|
||||||
|
"Something went wrong when processing the exception message, couldn't decode payload to utf-8.")
|
||||||
|
|
||||||
|
def process_in_queue(self, client, msg_in_queue):
|
||||||
|
while True:
|
||||||
|
msg = msg_in_queue.get()
|
||||||
|
client.publish(msg[0], msg[1])
|
||||||
@@ -12,14 +12,14 @@ import ha_control
|
|||||||
|
|
||||||
class LovelaceUIPanel:
|
class LovelaceUIPanel:
|
||||||
|
|
||||||
def __init__(self, name_panel, settings_panel):
|
def __init__(self, name_panel, settings_panel, msg_out_queue):
|
||||||
self.name = name_panel
|
self.name = name_panel
|
||||||
self.settings = settings_panel
|
self.settings = settings_panel
|
||||||
|
self.msg_out_queue = msg_out_queue
|
||||||
self.sendTopic = self.settings["panelSendTopic"]
|
self.sendTopic = self.settings["panelSendTopic"]
|
||||||
self.recvTopic = self.settings["panelRecvTopic"]
|
self.recvTopic = self.settings["panelRecvTopic"]
|
||||||
self.model = self.settings.get("model", "eu")
|
self.model = self.settings.get("model", "eu")
|
||||||
|
|
||||||
|
|
||||||
self.current_card = None
|
self.current_card = None
|
||||||
self.privious_cards = []
|
self.privious_cards = []
|
||||||
self.cards = {}
|
self.cards = {}
|
||||||
@@ -94,7 +94,7 @@ class LovelaceUIPanel:
|
|||||||
for e in self.screensaver.entities:
|
for e in self.screensaver.entities:
|
||||||
e.prerender()
|
e.prerender()
|
||||||
|
|
||||||
libs.panel_cmd.page_type(self.sendTopic, "pageStartup")
|
libs.panel_cmd.page_type(self.msg_out_queue, self.sendTopic, "pageStartup")
|
||||||
|
|
||||||
|
|
||||||
def schedule_thread_target(self):
|
def schedule_thread_target(self):
|
||||||
@@ -106,13 +106,13 @@ class LovelaceUIPanel:
|
|||||||
use_timezone = tz.gettz(self.settings["timeZone"])
|
use_timezone = tz.gettz(self.settings["timeZone"])
|
||||||
time_string = datetime.datetime.now(
|
time_string = datetime.datetime.now(
|
||||||
use_timezone).strftime(self.settings["timeFormat"])
|
use_timezone).strftime(self.settings["timeFormat"])
|
||||||
libs.panel_cmd.send_time(self.sendTopic, time_string)
|
libs.panel_cmd.send_time(self.msg_out_queue, self.sendTopic, time_string)
|
||||||
|
|
||||||
def update_date(self):
|
def update_date(self):
|
||||||
dateformat = self.settings["dateFormat"]
|
dateformat = self.settings["dateFormat"]
|
||||||
date_string = babel.dates.format_date(
|
date_string = babel.dates.format_date(
|
||||||
datetime.datetime.now(), dateformat, locale=self.settings["locale"])
|
datetime.datetime.now(), dateformat, locale=self.settings["locale"])
|
||||||
libs.panel_cmd.send_date(self.sendTopic, date_string)
|
libs.panel_cmd.send_date(self.msg_out_queue, self.sendTopic, date_string)
|
||||||
|
|
||||||
def searchCard(self, iid):
|
def searchCard(self, iid):
|
||||||
if iid in self.navigate_keys:
|
if iid in self.navigate_keys:
|
||||||
@@ -140,9 +140,9 @@ class LovelaceUIPanel:
|
|||||||
if etype=="light":
|
if etype=="light":
|
||||||
effectList = e.config.get("effectList")
|
effectList = e.config.get("effectList")
|
||||||
if etype in ['input_select', 'media_player']:
|
if etype in ['input_select', 'media_player']:
|
||||||
libs.panel_cmd.entityUpdateDetail2(self.sendTopic, detail_open(self.settings["locale"], etype, entity_id, entity_id_iid, sendTopic=self.sendTopic, options_list=effectList))
|
libs.panel_cmd.entityUpdateDetail2(self.msg_out_queue, self.sendTopic, detail_open(self.settings["locale"], etype, entity_id, entity_id_iid, self.msg_out_queue, sendTopic=self.sendTopic, options_list=effectList))
|
||||||
else:
|
else:
|
||||||
libs.panel_cmd.entityUpdateDetail(self.sendTopic, detail_open(self.settings["locale"], etype, entity_id, entity_id_iid, sendTopic=self.sendTopic, options_list=effectList))
|
libs.panel_cmd.entityUpdateDetail(self.msg_out_queue, self.sendTopic, detail_open(self.settings["locale"], etype, entity_id, entity_id_iid, self.msg_out_queue, sendTopic=self.sendTopic, options_list=effectList))
|
||||||
|
|
||||||
involved_entities = ha_control.calculate_dim_values(
|
involved_entities = ha_control.calculate_dim_values(
|
||||||
self.settings.get("sleepTracking"),
|
self.settings.get("sleepTracking"),
|
||||||
@@ -160,15 +160,9 @@ class LovelaceUIPanel:
|
|||||||
if not self.current_card:
|
if not self.current_card:
|
||||||
return
|
return
|
||||||
if switchPages:
|
if switchPages:
|
||||||
libs.panel_cmd.page_type(self.sendTopic, self.current_card.type)
|
libs.panel_cmd.page_type(self.msg_out_queue, self.sendTopic, self.current_card.type)
|
||||||
if requested:
|
if requested:
|
||||||
self.current_card.render()
|
self.current_card.render()
|
||||||
# send sleepTimeout
|
|
||||||
#sleepTimeout = self.settings.get("sleepTimeout", 20)
|
|
||||||
#if self.current_card.config.get("sleepTimeout"):
|
|
||||||
# sleepTimeout = self.current_card.config.get("sleepTimeout")
|
|
||||||
#libs.panel_cmd.timeout(self.sendTopic, sleepTimeout)
|
|
||||||
#self.dimmode()
|
|
||||||
|
|
||||||
def dimmode(self):
|
def dimmode(self):
|
||||||
# send dimmode
|
# send dimmode
|
||||||
@@ -187,7 +181,7 @@ class LovelaceUIPanel:
|
|||||||
backgroundColor = 0
|
backgroundColor = 0
|
||||||
fontColor = ""
|
fontColor = ""
|
||||||
featExperimentalSliders = self.settings.get("featExperimentalSliders", 0)
|
featExperimentalSliders = self.settings.get("featExperimentalSliders", 0)
|
||||||
libs.panel_cmd.dimmode(self.sendTopic, dimValue, dimValueNormal, backgroundColor, fontColor, featExperimentalSliders)
|
libs.panel_cmd.dimmode(self.msg_out_queue, self.sendTopic, dimValue, dimValueNormal, backgroundColor, fontColor, featExperimentalSliders)
|
||||||
|
|
||||||
def get_default_card(self):
|
def get_default_card(self):
|
||||||
defaultCard = self.settings.get("defaultCard")
|
defaultCard = self.settings.get("defaultCard")
|
||||||
@@ -214,7 +208,7 @@ class LovelaceUIPanel:
|
|||||||
sleepTimeout = self.settings.get("sleepTimeout", 20)
|
sleepTimeout = self.settings.get("sleepTimeout", 20)
|
||||||
if self.current_card.config.get("sleepTimeout"):
|
if self.current_card.config.get("sleepTimeout"):
|
||||||
sleepTimeout = self.current_card.config.get("sleepTimeout")
|
sleepTimeout = self.current_card.config.get("sleepTimeout")
|
||||||
libs.panel_cmd.timeout(self.sendTopic, sleepTimeout)
|
libs.panel_cmd.timeout(self.msg_out_queue, self.sendTopic, sleepTimeout)
|
||||||
self.dimmode()
|
self.dimmode()
|
||||||
|
|
||||||
if msg[1] == "sleepReached":
|
if msg[1] == "sleepReached":
|
||||||
@@ -288,8 +282,8 @@ class LovelaceUIPanel:
|
|||||||
if entity_id.startswith("light"):
|
if entity_id.startswith("light"):
|
||||||
effectList = e.config.get("effectList")
|
effectList = e.config.get("effectList")
|
||||||
if entity_id.split(".")[0] in ['input_select', 'media_player']:
|
if entity_id.split(".")[0] in ['input_select', 'media_player']:
|
||||||
libs.panel_cmd.entityUpdateDetail2(self.sendTopic, detail_open(self.settings["locale"], msg[2], entity_id, msg[3], sendTopic=self.sendTopic, options_list=effectList))
|
libs.panel_cmd.entityUpdateDetail2(self.msg_out_queue, self.sendTopic, detail_open(self.settings["locale"], msg[2], entity_id, msg[3], self.msg_out_queue, sendTopic=self.sendTopic, options_list=effectList))
|
||||||
else:
|
else:
|
||||||
libs.panel_cmd.entityUpdateDetail(self.sendTopic, detail_open(self.settings["locale"], msg[2], entity_id, msg[3], sendTopic=self.sendTopic))
|
libs.panel_cmd.entityUpdateDetail(self.msg_out_queue, self.sendTopic, detail_open(self.settings["locale"], msg[2], entity_id, msg[3], self.msg_out_queue, sendTopic=self.sendTopic))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user