This commit is contained in:
joBr99
2023-11-19 18:29:12 +01:00
parent 5d3e836156
commit 431a93a7ff
4 changed files with 19 additions and 52 deletions

View File

@@ -1,7 +1,6 @@
nspanels: nspanels:
tasmota_nspdev2: tasmota_nspdev2:
name: test name: test
mac: AA:BB:CC:DD:EE:FF
panelRecvTopic: "tele/tasmota_nspdev2/RESULT" panelRecvTopic: "tele/tasmota_nspdev2/RESULT"
panelSendTopic: "cmnd/tasmota_nspdev2/CustomSend" panelSendTopic: "cmnd/tasmota_nspdev2/CustomSend"
timeFormat: "%H:%M" timeFormat: "%H:%M"

View File

@@ -21,16 +21,17 @@ def on_off(entity_id, value):
def button_press(entity_id, value): def button_press(entity_id, value):
etype = entity_id.split["."][0] etype = entity_id.split(".")[0]
match etype: match etype:
case 'scene': case 'scene' | 'script':
libs.home_assistant.call_service( libs.home_assistant.call_service(
entity_name=entity_id, entity_name=entity_id,
domain="scene", domain=etype,
service="turn_on", service="turn_on",
service_data={} service_data={}
) )
# apis.ha_api.log( # apis.ha_api.log(
# f"Button Press Event; entity_id: {entity_id}; button_type: {button_type}; value: {value} ") # f"Button Press Event; entity_id: {entity_id}; button_type: {button_type}; value: {value} ")
# # buttons with actions on HA # # buttons with actions on HA
@@ -73,41 +74,6 @@ def button_press(entity_id, value):
# "set_cover_tilt_position", tilt_position=pos) # "set_cover_tilt_position", tilt_position=pos)
# #
# if button_type == "button": # if button_type == "button":
# if entity_id.startswith('navigate'):
# # internal navigation for next/prev
# if entity_id.startswith('navigate.uuid'):
# dstCard = self._config.get_card_by_uuid(
# entity_id.replace('navigate.', ''))
# # internal for navigation to nested pages
# else:
# dstCard = self._config.search_card(entity_id)
# if dstCard is not None:
# if dstCard.hidden:
# self._previous_cards.append(self._current_card)
# self._current_card = dstCard
# self._pages_gen.render_card(self._current_card)
# else:
# apis.ha_api.log(f"No page with key {entity_id} found")
# if entity_id.startswith('navUp'):
# if self._previous_cards:
# self._current_card = self._previous_cards.pop()
# else:
# self._current_card = self._config.get_default_card()
# self._pages_gen.render_card(self._current_card)
# if entity_id.startswith('navPrev'):
# if self._current_card.uuid_prev:
# self._current_card = self._config.get_card_by_uuid(
# self._current_card.uuid_prev)
# self._pages_gen.render_card(self._current_card)
# if entity_id.startswith('navNext'):
# if self._current_card.uuid_next:
# self._current_card = self._config.get_card_by_uuid(
# self._current_card.uuid_next)
# self._pages_gen.render_card(self._current_card)
# elif entity_id.startswith('scene'):
# apis.ha_api.get_entity(entity_id).call_service("turn_on")
# elif entity_id.startswith('script'):
# apis.ha_api.get_entity(entity_id).call_service("turn_on")
# elif entity_id.startswith('light') or entity_id.startswith('switch') or entity_id.startswith('input_boolean') or entity_id.startswith('automation') or entity_id.startswith('fan'): # elif entity_id.startswith('light') or entity_id.startswith('switch') or entity_id.startswith('input_boolean') or entity_id.startswith('automation') or entity_id.startswith('fan'):
# apis.ha_api.get_entity(entity_id).call_service("toggle") # apis.ha_api.get_entity(entity_id).call_service("toggle")
# elif entity_id.startswith('lock'): # elif entity_id.startswith('lock'):

View File

@@ -19,10 +19,10 @@ ON_CONNECT_HANDLER = None
ON_DISCONNECT_HANDLER = None ON_DISCONNECT_HANDLER = None
def init(settings_from_manager, mqtt_client_from_manager): def init(settings_from_manager, on_ha_update_from_manager):
global home_assistant_url, home_assistant_token, settings, mqtt_client global home_assistant_url, home_assistant_token, settings, on_ha_update
settings = settings_from_manager settings = settings_from_manager
mqtt_client = mqtt_client_from_manager on_ha_update = on_ha_update_from_manager
home_assistant_url = settings["home_assistant_address"] home_assistant_url = settings["home_assistant_address"]
home_assistant_token = settings["home_assistant_token"] home_assistant_token = settings["home_assistant_token"]
# Disable logging from underlying "websocket" # Disable logging from underlying "websocket"
@@ -92,11 +92,10 @@ def connect():
def _do_connection(): def _do_connection():
global home_assistant_url, ws global home_assistant_url, ws, settings
ws_url = home_assistant_url.replace( ws_url = home_assistant_url.replace(
"https://", "wss://").replace("http://", "ws://") "https://", "wss://").replace("http://", "ws://")
environment = environ.Env() if settings["is_addon"]:
if "IS_HOME_ASSISTANT_ADDON" in environment and environment("IS_HOME_ASSISTANT_ADDON") == "true":
ws_url += "/core/websocket" ws_url += "/core/websocket"
else: else:
ws_url += "/api/websocket" ws_url += "/api/websocket"
@@ -142,11 +141,9 @@ def _get_all_states():
# Got new value from Home Assistant, publish to MQTT # Got new value from Home Assistant, publish to MQTT
def send_entity_update(json_msg): def send_entity_update(entity_id):
global mqtt_client global on_ha_update
# Check if the light is used on any nspanel and if so, send MQTT state update on_ha_update(entity_id)
# mqtt_client.publish(f"cmnd/tasmota_nspdev2/CustomSend", "page~screensaver")
def set_entity_brightness(home_assistant_name: str, light_level: int, color_temp: int) -> bool: def set_entity_brightness(home_assistant_name: str, light_level: int, color_temp: int) -> bool:
"""Set entity brightness""" """Set entity brightness"""

View File

@@ -30,6 +30,8 @@ def on_connect(client, userdata, flags, rc):
# client.subscribe(panel["panelRecvTopic"]) # client.subscribe(panel["panelRecvTopic"])
client.subscribe("tele/tasmota_nspdev2/RESULT") client.subscribe("tele/tasmota_nspdev2/RESULT")
def on_ha_update(entity_id):
logging.debug(f"{entity_id} updated/state changed")
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
try: try:
@@ -78,9 +80,13 @@ def get_config():
if not settings.get("mqtt_server"): if not settings.get("mqtt_server"):
settings["mqtt_server"] = environment('MQTT_SERVER') settings["mqtt_server"] = environment('MQTT_SERVER')
settings["is_addon"] = False
if "SUPERVISOR_TOKEN" in environment: if "SUPERVISOR_TOKEN" in environment:
settings["home_assistant_token"] = environment('SUPERVISOR_TOKEN') settings["home_assistant_token"] = environment('SUPERVISOR_TOKEN')
print(settings["home_assistant_token"])
settings["home_assistant_address"] = "http://supervisor" settings["home_assistant_address"] = "http://supervisor"
settings["is_addon"] = True
def connect_and_loop(): def connect_and_loop():
@@ -106,7 +112,7 @@ def connect_and_loop():
# MQTT Connected, start APIs if configured # MQTT Connected, start APIs if configured
if settings["home_assistant_address"] != "" and settings["home_assistant_token"] != "": if settings["home_assistant_address"] != "" and settings["home_assistant_token"] != "":
libs.home_assistant.init(settings, client) libs.home_assistant.init(settings, on_ha_update)
libs.home_assistant.connect() libs.home_assistant.connect()
else: else:
logging.info("Home Assistant values not configured, will not connect.") logging.info("Home Assistant values not configured, will not connect.")
@@ -122,7 +128,6 @@ def connect_and_loop():
# Loop MQTT # Loop MQTT
client.loop_forever() client.loop_forever()
if __name__ == '__main__': if __name__ == '__main__':
get_config() get_config()
# print(settings) # print(settings)