From 694b2319834fd27d2d0eb1f6be2780c1aa1180ec Mon Sep 17 00:00:00 2001 From: joBr99 <29555657+joBr99@users.noreply.github.com> Date: Mon, 20 Nov 2023 20:02:53 +0100 Subject: [PATCH] implement sleepmodes --- .../rootfs/usr/bin/mqtt-manager/ha_control.py | 42 +++++++++++++++++++ .../bin/mqtt-manager/libs/home_assistant.py | 5 +++ .../usr/bin/mqtt-manager/libs/panel_cmd.py | 2 + .../rootfs/usr/bin/mqtt-manager/panel.py | 31 +++++++++++++- 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_control.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_control.py index 244d74f1..c1642046 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_control.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/ha_control.py @@ -8,6 +8,48 @@ def wait_for_ha_cache(): if len(libs.home_assistant.home_assistant_entity_state_cache) == 0: time.sleep(0.1) +def calculate_dim_values(sleepTracking, sleepTrackingZones, sleepBrightness, screenBrightness, sleepOverride, return_involved_entities=False): + dimmode = 10 + dimValueNormal = 100 + involved_entities = [] + + if sleepBrightness: + if isinstance(sleepBrightness, int): + dimmode = sleepBrightness + elif libs.home_assistant.is_existent(sleepBrightness): + involved_entities.append(sleepBrightness) + dimValueNormal = int(libs.home_assistant.get_entity_data(sleepBrightness).get('state', 10)) + + if screenBrightness: + if isinstance(screenBrightness, int): + dimValueNormal = screenBrightness + elif libs.home_assistant.is_existent(screenBrightness): + involved_entities.append(screenBrightness) + dimValueNormal = int(libs.home_assistant.get_entity_data(screenBrightness).get('state', 100)) + + # force sleep brightness to zero in case sleepTracking is active + if sleepTracking: + if libs.home_assistant.is_existent(sleepTracking): + involved_entities.append(sleepTracking) + state = libs.home_assistant.get_entity_data(sleepTracking).get('state', '') + if state in sleepTrackingZones: + logging.info("sleepTracking active forcing brightnesss to 0") + dimmode = 0 + + # overwrite everything with sleepOverwrite + if sleepOverride and isinstance(sleepOverride, dict) and sleepOverride.get("entity") and sleepOverride.get("brightness"): + entity = sleepOverride.get("entity") + if libs.home_assistant.is_existent(entity): + involved_entities.append(entity) + state = libs.home_assistant.get_entity_data(entity).get('state', '') + if state in ["on", "true", "home"]: + dimmode = sleepOverride.get("brightness") + + if return_involved_entities: + return involved_entities + else: + return dimmode, dimValueNormal + def handle_buttons(entity_id, btype, value): match btype: case 'button': diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/home_assistant.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/home_assistant.py index a163bc90..0a241f51 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/home_assistant.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/home_assistant.py @@ -208,6 +208,11 @@ def get_entity_data(entity_id: str): return home_assistant_entity_state_cache[entity_id] else: return None +def is_existent(entity_id: str): + if entity_id in home_assistant_entity_state_cache: + return True + else: + return False def send_message(message): diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/panel_cmd.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/panel_cmd.py index 22f499fd..fe7a1fa5 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/panel_cmd.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/libs/panel_cmd.py @@ -35,4 +35,6 @@ def timeout(topic, timeout): custom_send(topic, f"timeout~{timeout}") def dimmode(topic, dimValue, dimValueNormal, backgroundColor, fontColor, featExperimentalSliders): + if dimValue==dimValueNormal: + dimValue=dimValue-1 custom_send(topic, f"dimmode~{dimValue}~{dimValueNormal}~{backgroundColor}~{fontColor}~{featExperimentalSliders}") \ No newline at end of file diff --git a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel.py b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel.py index 7428adb9..256b8dc0 100644 --- a/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel.py +++ b/nspanel-lovelace-ui/rootfs/usr/bin/mqtt-manager/panel.py @@ -99,6 +99,18 @@ class LovelaceUIPanel: if entity_id in self.current_card.get_entities(): self.render_current_page() + involved_entities = ha_control.calculate_dim_values( + self.settings.get("sleepTracking"), + self.settings.get("sleepTrackingZones", ["not_home", "off"]), + self.settings.get("sleepBrightness"), + self.settings.get("screenBrightness"), + self.settings.get("sleepOverride"), + return_involved_entities=True + ) + if entity_id in involved_entities: + dimmode() + + def render_current_page(self, switchPages=False): if switchPages: libs.panel_cmd.page_type(self.sendTopic, self.current_card.type) @@ -112,9 +124,26 @@ class LovelaceUIPanel: if self.current_card.config.get("sleepTimeout"): sleepTimeout = self.current_card.config.get("sleepTimeout") libs.panel_cmd.timeout(self.sendTopic, sleepTimeout) + dimmode() + def dimmode(): # send dimmode - #libs.panel_cmd.dimmode(self.sendTopic, 10, 100, ) + dimValue, dimValueNormal = ha_control.calculate_dim_values( + self.settings.get("sleepTracking"), + self.settings.get("sleepTrackingZones", ["not_home", "off"]), + self.settings.get("sleepBrightness"), + self.settings.get("screenBrightness"), + self.settings.get("sleepOverride"), + ) + + backgroundColor = self.settings.get("defaultBackgroundColor", "ha-dark") + if backgroundColor == "ha-dark": + backgroundColor = 6371 + elif backgroundColor == "black": + backgroundColor = 0 + fontColor = "" + featExperimentalSliders = self.settings.get("featExperimentalSliders", 0) + libs.panel_cmddimmode(self.sendTopic, dimValue, dimValueNormal, backgroundColor, fontColor, featExperimentalSliders) def customrecv_event_callback(self, msg):