From fe3e0c882e1ed65518bb10b424d6daeaba20c7e0 Mon Sep 17 00:00:00 2001 From: Johannes Braun Date: Sun, 20 Mar 2022 13:54:03 +0100 Subject: [PATCH] migrate weather to icon mapper --- HMI/code_gen/icons/icons.py | 2 +- .../{color.py => helper.py} | 6 ++ apps/nspanel-lovelace-ui/icon_mapper.py | 2 +- .../nspanel-lovelace-ui.py | 58 +++++++++---------- 4 files changed, 35 insertions(+), 33 deletions(-) rename apps/nspanel-lovelace-ui/{color.py => helper.py} (81%) diff --git a/HMI/code_gen/icons/icons.py b/HMI/code_gen/icons/icons.py index 7b92b7de..3a8ce515 100644 --- a/HMI/code_gen/icons/icons.py +++ b/HMI/code_gen/icons/icons.py @@ -68,7 +68,7 @@ with open(os.path.join(__location__, "../../../apps/nspanel-lovelace-ui", "icon_ f.write(f" '{val}': {idx},\n") f.write("}\n") f.write(""" -def get_icon(ma_name): +def get_icon_id(ma_name): if ma_name in icons: return icons[ma_name] else: diff --git a/apps/nspanel-lovelace-ui/color.py b/apps/nspanel-lovelace-ui/helper.py similarity index 81% rename from apps/nspanel-lovelace-ui/color.py rename to apps/nspanel-lovelace-ui/helper.py index 0ca15c05..2609eed5 100644 --- a/apps/nspanel-lovelace-ui/color.py +++ b/apps/nspanel-lovelace-ui/helper.py @@ -1,6 +1,12 @@ import colorsys import math + def scale(val, src, dst): + """ + Scale the given value from the scale of src to the scale of dst. + """ + return ((val - src[0]) / (src[1]-src[0])) * (dst[1]-dst[0]) + dst[0] + def hsv2rgb(h, s, v): hsv = colorsys.hsv_to_rgb(h,s,v) return tuple(round(i * 255) for i in hsv) diff --git a/apps/nspanel-lovelace-ui/icon_mapper.py b/apps/nspanel-lovelace-ui/icon_mapper.py index 1dc13daf..d69f5f52 100644 --- a/apps/nspanel-lovelace-ui/icon_mapper.py +++ b/apps/nspanel-lovelace-ui/icon_mapper.py @@ -36,7 +36,7 @@ icons = { 'battery-medium': 34, } -def get_icon(ma_name): +def get_icon_id(ma_name): if ma_name in icons: return icons[ma_name] else: diff --git a/apps/nspanel-lovelace-ui/nspanel-lovelace-ui.py b/apps/nspanel-lovelace-ui/nspanel-lovelace-ui.py index 9381a86c..e75a6f0c 100644 --- a/apps/nspanel-lovelace-ui/nspanel-lovelace-ui.py +++ b/apps/nspanel-lovelace-ui/nspanel-lovelace-ui.py @@ -1,7 +1,8 @@ import json import datetime import hassapi as hass -from color import pos_to_color, rgb_dec565, rgb_brightness +from helper import scale, pos_to_color, rgb_dec565, rgb_brightness +from icon_mapper import get_icon_id # check Babel import importlib @@ -68,6 +69,10 @@ class NsPanelLovelaceUI: # register callbacks self.register_callbacks() + def send_mqtt_msg(self,msg): + self.api.log("Send Message from Tasmota: %s", msg) #, level="DEBUG" + self.mqtt.mqtt_publish(self.config["panelSendTopic"], msg) + def handle_mqtt_incoming_message(self, event_name, data, kwargs): # Parse Json Message from Tasmota and strip out message from nextion display data = json.loads(data["payload"]) @@ -133,10 +138,6 @@ class NsPanelLovelaceUI: if msg[1] == "screensaverOpen": self.update_screensaver_weather("") - def send_mqtt_msg(self,msg): - self.api.log("Send Message from Tasmota: %s", msg) #, level="DEBUG" - self.mqtt.mqtt_publish(self.config["panelSendTopic"], msg) - def update_time(self, kwargs): time = datetime.datetime.now().strftime(self.config["timeFormat"]) self.send_mqtt_msg(f"time,{time}") @@ -166,22 +167,23 @@ class NsPanelLovelaceUI: we = self.api.get_entity(self.config["weatherEntity"]) unit = "°C" + # this maps possible states from ha to material design icon names weathericons = { - 'clear-night': 17, - 'cloudy': 12, - 'exceptional': 11, - 'fog': 13, - 'hail': 14, - 'lightning': 15, - 'lightning-rainy': 16, - 'partlycloudy': 18, - 'pouring': 19, - 'rainy': 20, - 'snowy': 21, - 'snowy-rainy': 22, - 'sunny': 23, - 'windy': 24, - 'windy-variant': 25 + 'clear-night': 'weather-night', + 'cloudy': 'weather-cloudy', + 'exceptional': 'alert-circle-outline', + 'fog': 'weather-fog', + 'hail': 'weather-hail', + 'lightning': 'weather-lightning', + 'lightning-rainy': 'weather-lightning-rainy', + 'partlycloudy': 'weather-partly-cloudy', + 'pouring': 'weather-pouring', + 'rainy': 'weather-rainy', + 'snowy': 'weather-snowy', + 'snowy-rainy': 'weather-snowy-rainy', + 'sunny': 'weather-sunny', + 'windy': 'weather-windy', + 'windy-variant': 'weather-windy-variant' } o1 = we.attributes.forecast[0]['datetime'] @@ -194,13 +196,7 @@ class NsPanelLovelaceUI: o2 = babel.dates.format_date(o2, "E", locale=self.config["locale"]) i2 = weathericons[we.attributes.forecast[1]['condition']] u2 = we.attributes.forecast[1]['temperature'] - self.send_mqtt_msg(f"weatherUpdate,?{weathericons[we.state]}?{we.attributes.temperature}{unit}?{26}?{we.attributes.humidity} %?{o1}?{i1}?{u1}?{o2}?{i2}?{u2}") - - def scale(self, val, src, dst): - """ - Scale the given value from the scale of src to the scale of dst. - """ - return ((val - src[0]) / (src[1]-src[0])) * (dst[1]-dst[0]) + dst[0] + self.send_mqtt_msg(f"weatherUpdate,?{get_icon_id(weathericons[we.state])}?{we.attributes.temperature}{unit}?{26}?{we.attributes.humidity} %?{o1}?{i1}?{u1}?{o2}?{i2}?{u2}") def handle_button_press(self, entity_id, btype, optVal=None): if(btype == "OnOff"): @@ -236,13 +232,13 @@ class NsPanelLovelaceUI: if(btype == "brightnessSlider"): # scale 0-100 to ha brightness range - brightness = int(self.scale(int(optVal),(0,100),(0,255))) + brightness = int(scale(int(optVal),(0,100),(0,255))) self.api.get_entity(entity_id).call_service("turn_on", brightness=brightness) if(btype == "colorTempSlider"): entity = self.api.get_entity(entity_id) #scale 0-100 from slider to color range of lamp - color_val = self.scale(int(optVal), (0, 100), (entity.attributes.min_mireds, entity.attributes.max_mireds)) + color_val = scale(int(optVal), (0, 100), (entity.attributes.min_mireds, entity.attributes.max_mireds)) self.api.get_entity(entity_id).call_service("turn_on", color_temp=color_val) if(btype == "colorWheel"): @@ -550,13 +546,13 @@ class NsPanelLovelaceUI: # scale 0-255 brightness from ha to 0-100 if entity.state == "on": if "brightness" in entity.attributes: - brightness = int(self.scale(entity.attributes.brightness,(0,255),(0,100))) + brightness = int(scale(entity.attributes.brightness,(0,255),(0,100))) else: brightness = "disable" if "color_temp" in entity.attributes.supported_color_modes: if "color_temp" in entity.attributes: # scale ha color temp range to 0-100 - color_temp = int(self.scale(entity.attributes.color_temp,(entity.attributes.min_mireds, entity.attributes.max_mireds),(0,100))) + color_temp = int(scale(entity.attributes.color_temp,(entity.attributes.min_mireds, entity.attributes.max_mireds),(0,100))) else: color_temp = "unknown" else: