mirror of
https://github.com/joBr99/nspanel-lovelace-ui.git
synced 2026-02-18 19:06:58 +01:00
initial implementation of esphome api comm
This commit is contained in:
@@ -70,7 +70,8 @@ def on_message(ws, message):
|
|||||||
for template, template_cache_entry in template_cache.items():
|
for template, template_cache_entry in template_cache.items():
|
||||||
if entity_id in template_cache_entry.get("listener-entities", []):
|
if entity_id in template_cache_entry.get("listener-entities", []):
|
||||||
cache_template(template)
|
cache_template(template)
|
||||||
|
elif json_msg["type"] == "event" and json_msg["event"]["event_type"] == "esphome.nspanel.data":
|
||||||
|
nspanel_data_callback(json_msg["event"]["data"]["device_id"], json_msg["event"]["data"]["CustomRecv"])
|
||||||
elif json_msg["type"] == "result" and not json_msg["success"]:
|
elif json_msg["type"] == "result" and not json_msg["success"]:
|
||||||
logging.error("Failed result: ")
|
logging.error("Failed result: ")
|
||||||
logging.error(json_msg)
|
logging.error(json_msg)
|
||||||
@@ -143,6 +144,15 @@ def subscribe_to_events():
|
|||||||
}
|
}
|
||||||
send_message(json.dumps(msg))
|
send_message(json.dumps(msg))
|
||||||
|
|
||||||
|
def subscribe_to_nspanel_events(nsp_callback):
|
||||||
|
global next_id, nspanel_data_callback
|
||||||
|
nspanel_data_callback = nsp_callback
|
||||||
|
msg = {
|
||||||
|
"id": next_id,
|
||||||
|
"type": "subscribe_events",
|
||||||
|
"event_type": "esphome.nspanel.data"
|
||||||
|
}
|
||||||
|
send_message(json.dumps(msg))
|
||||||
|
|
||||||
def _get_all_states():
|
def _get_all_states():
|
||||||
global next_id, request_all_states_id
|
global next_id, request_all_states_id
|
||||||
@@ -158,6 +168,10 @@ def send_entity_update(entity_id):
|
|||||||
global on_ha_update
|
global on_ha_update
|
||||||
on_ha_update(entity_id)
|
on_ha_update(entity_id)
|
||||||
|
|
||||||
|
def nspanel_data_callback(device_id, msg):
|
||||||
|
global nspanel_data_callback
|
||||||
|
nspanel_data_callback(device_id, msg)
|
||||||
|
|
||||||
def call_service(entity_name: str, domain: str, service: str, service_data: dict) -> bool:
|
def call_service(entity_name: str, domain: str, service: str, service_data: dict) -> bool:
|
||||||
global next_id
|
global next_id
|
||||||
try:
|
try:
|
||||||
@@ -177,6 +191,22 @@ def call_service(entity_name: str, domain: str, service: str, service_data: dict
|
|||||||
logging.exception("Failed to call Home Assisatant service.")
|
logging.exception("Failed to call Home Assisatant service.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def send_msg_to_panel(service: str, service_data: dict) -> bool:
|
||||||
|
global next_id
|
||||||
|
try:
|
||||||
|
msg = {
|
||||||
|
"id": next_id,
|
||||||
|
"type": "call_service",
|
||||||
|
"domain": "esphome",
|
||||||
|
"service": service,
|
||||||
|
"service_data": service_data,
|
||||||
|
}
|
||||||
|
send_message(json.dumps(msg))
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logging.exception("Failed to call Home Assisatant service.")
|
||||||
|
return False
|
||||||
|
|
||||||
def execute_script(entity_name: str, domain: str, service: str, service_data: dict) -> str:
|
def execute_script(entity_name: str, domain: str, service: str, service_data: dict) -> str:
|
||||||
global next_id, response_buffer
|
global next_id, response_buffer
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -33,9 +33,33 @@ def on_ha_update(entity_id):
|
|||||||
for queue in panel_in_queues.values():
|
for queue in panel_in_queues.values():
|
||||||
queue.put(("HA:", entity_id))
|
queue.put(("HA:", entity_id))
|
||||||
|
|
||||||
|
def on_ha_panel_event(device_id, msg):
|
||||||
|
global panel_in_queues
|
||||||
|
|
||||||
|
if device_id in panel_in_queues.keys():
|
||||||
|
queue = panel_in_queues[device_id]
|
||||||
|
queue.put(("MQTT:", msg))
|
||||||
|
|
||||||
|
def process_output_to_panel():
|
||||||
|
while True:
|
||||||
|
msg = panel_out_queue.get()
|
||||||
|
|
||||||
|
#client.publish(msg[0], msg[1])
|
||||||
|
#apis.ha_api.call_service(service="esphome/" + self._api_panel_name + "_nspanelui_api_call", command=2, data=msg)
|
||||||
|
service = msg[0] + "_nspanelui_api_call"
|
||||||
|
service_data = {
|
||||||
|
"data": msg[1],
|
||||||
|
"command":2
|
||||||
|
}
|
||||||
|
libs.home_assistant.send_msg_to_panel(
|
||||||
|
service = service,
|
||||||
|
service_data = service_data
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def connect():
|
def connect():
|
||||||
global settings, panel_out_queue
|
global settings, panel_out_queue
|
||||||
if settings["mqtt_server"] != "":
|
if settings["mqtt_server"]:
|
||||||
MqttManager(settings, panel_out_queue, panel_in_queues)
|
MqttManager(settings, panel_out_queue, panel_in_queues)
|
||||||
else:
|
else:
|
||||||
logging.info("MQTT values not configured, will not connect.")
|
logging.info("MQTT values not configured, will not connect.")
|
||||||
@@ -47,6 +71,14 @@ def connect():
|
|||||||
else:
|
else:
|
||||||
logging.info("Home Assistant values not configured, will not connect.")
|
logging.info("Home Assistant values not configured, will not connect.")
|
||||||
|
|
||||||
|
while not libs.home_assistant.ws_connected:
|
||||||
|
time.sleep(1)
|
||||||
|
if settings["use_ha_api"]:
|
||||||
|
libs.home_assistant.subscribe_to_nspanel_events(on_ha_panel_event)
|
||||||
|
send_to_panel_thread = threading.Thread(target=process_output_to_panel, args=())
|
||||||
|
send_to_panel_thread.daemon = True
|
||||||
|
send_to_panel_thread.start()
|
||||||
|
|
||||||
def setup_panels():
|
def setup_panels():
|
||||||
global settings, panel_in_queues
|
global settings, panel_in_queues
|
||||||
# Create NsPanel object
|
# Create NsPanel object
|
||||||
|
|||||||
Reference in New Issue
Block a user