implement sleepmodes

This commit is contained in:
joBr99
2023-11-20 20:02:53 +01:00
parent 3e56cf7d8c
commit 694b231983
4 changed files with 79 additions and 1 deletions

View File

@@ -8,6 +8,48 @@ def wait_for_ha_cache():
if len(libs.home_assistant.home_assistant_entity_state_cache) == 0: if len(libs.home_assistant.home_assistant_entity_state_cache) == 0:
time.sleep(0.1) 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): def handle_buttons(entity_id, btype, value):
match btype: match btype:
case 'button': case 'button':

View File

@@ -208,6 +208,11 @@ def get_entity_data(entity_id: str):
return home_assistant_entity_state_cache[entity_id] return home_assistant_entity_state_cache[entity_id]
else: else:
return None 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): def send_message(message):

View File

@@ -35,4 +35,6 @@ def timeout(topic, timeout):
custom_send(topic, f"timeout~{timeout}") custom_send(topic, f"timeout~{timeout}")
def dimmode(topic, dimValue, dimValueNormal, backgroundColor, fontColor, featExperimentalSliders): def dimmode(topic, dimValue, dimValueNormal, backgroundColor, fontColor, featExperimentalSliders):
if dimValue==dimValueNormal:
dimValue=dimValue-1
custom_send(topic, f"dimmode~{dimValue}~{dimValueNormal}~{backgroundColor}~{fontColor}~{featExperimentalSliders}") custom_send(topic, f"dimmode~{dimValue}~{dimValueNormal}~{backgroundColor}~{fontColor}~{featExperimentalSliders}")

View File

@@ -99,6 +99,18 @@ class LovelaceUIPanel:
if entity_id in self.current_card.get_entities(): if entity_id in self.current_card.get_entities():
self.render_current_page() 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): def render_current_page(self, switchPages=False):
if switchPages: if switchPages:
libs.panel_cmd.page_type(self.sendTopic, self.current_card.type) libs.panel_cmd.page_type(self.sendTopic, self.current_card.type)
@@ -112,9 +124,26 @@ class LovelaceUIPanel:
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.sendTopic, sleepTimeout)
dimmode()
def dimmode():
# send 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): def customrecv_event_callback(self, msg):