mirror of
https://github.com/joBr99/nspanel-lovelace-ui.git
synced 2026-02-17 10:27:00 +01:00
Add localization for weather forecast
Show the day in local format for the weather forecast
This commit is contained in:
@@ -4,6 +4,13 @@ import hassapi as hass
|
|||||||
import math
|
import math
|
||||||
import colorsys
|
import colorsys
|
||||||
|
|
||||||
|
# check Babel
|
||||||
|
import importlib
|
||||||
|
babel_spec = importlib.util.find_spec("babel")
|
||||||
|
if babel_spec is not None:
|
||||||
|
import babel.dates
|
||||||
|
|
||||||
|
|
||||||
class NsPanelLovelaceUIManager(hass.Hass):
|
class NsPanelLovelaceUIManager(hass.Hass):
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
|
|
||||||
@@ -20,6 +27,8 @@ class NsPanelLovelaceUI:
|
|||||||
# check configured items
|
# check configured items
|
||||||
self.check_items()
|
self.check_items()
|
||||||
|
|
||||||
|
self.babel_spec = importlib.util.find_spec("babel")
|
||||||
|
|
||||||
# Setup, mqtt subscription and callback
|
# Setup, mqtt subscription and callback
|
||||||
self.mqtt = self.api.get_plugin_api("MQTT")
|
self.mqtt = self.api.get_plugin_api("MQTT")
|
||||||
self.mqtt.mqtt_subscribe(topic=self.config["panelRecvTopic"])
|
self.mqtt.mqtt_subscribe(topic=self.config["panelRecvTopic"])
|
||||||
@@ -140,12 +149,8 @@ class NsPanelLovelaceUI:
|
|||||||
self.send_mqtt_msg("time,{0}".format(time))
|
self.send_mqtt_msg("time,{0}".format(time))
|
||||||
|
|
||||||
def update_date(self, kwargs):
|
def update_date(self, kwargs):
|
||||||
|
if self.babel_spec is not None:
|
||||||
import importlib
|
self.api.log("Babel package found", level="DEBUG")
|
||||||
babel_spec = importlib.util.find_spec("babel")
|
|
||||||
if babel_spec is not None:
|
|
||||||
self.api.log("babel package found", level="DEBUG")
|
|
||||||
import babel.dates
|
|
||||||
if "dateFormatBabel" in self.config:
|
if "dateFormatBabel" in self.config:
|
||||||
dateformat = self.config["dateFormatBabel"]
|
dateformat = self.config["dateFormatBabel"]
|
||||||
else:
|
else:
|
||||||
@@ -153,7 +158,7 @@ class NsPanelLovelaceUI:
|
|||||||
date = babel.dates.format_date(datetime.datetime.now(), dateformat, locale=self.config["locale"])
|
date = babel.dates.format_date(datetime.datetime.now(), dateformat, locale=self.config["locale"])
|
||||||
self.send_mqtt_msg(f"date,?{date}")
|
self.send_mqtt_msg(f"date,?{date}")
|
||||||
else:
|
else:
|
||||||
self.api.log("babel package not found", level="DEBUG")
|
self.api.log("Babel package not found", level="DEBUG")
|
||||||
date = datetime.datetime.now().strftime(self.config["dateFormat"])
|
date = datetime.datetime.now().strftime(self.config["dateFormat"])
|
||||||
self.send_mqtt_msg(f"date,?{date}")
|
self.send_mqtt_msg(f"date,?{date}")
|
||||||
|
|
||||||
@@ -187,12 +192,12 @@ class NsPanelLovelaceUI:
|
|||||||
|
|
||||||
o1 = we.attributes.forecast[0]['datetime']
|
o1 = we.attributes.forecast[0]['datetime']
|
||||||
o1 = datetime.datetime.fromisoformat(o1)
|
o1 = datetime.datetime.fromisoformat(o1)
|
||||||
o1 = o1.strftime("%a")
|
o1 = babel.dates.format_date(o1, "E", locale=self.config["locale"])
|
||||||
i1 = weathericons[we.attributes.forecast[0]['condition']]
|
i1 = weathericons[we.attributes.forecast[0]['condition']]
|
||||||
u1 = we.attributes.forecast[0]['temperature']
|
u1 = we.attributes.forecast[0]['temperature']
|
||||||
o2 = we.attributes.forecast[1]['datetime']
|
o2 = we.attributes.forecast[1]['datetime']
|
||||||
o2 = datetime.datetime.fromisoformat(o2)
|
o2 = datetime.datetime.fromisoformat(o2)
|
||||||
o2 = o2.strftime("%a")
|
o2 = babel.dates.format_date(o2, "E", locale=self.config["locale"])
|
||||||
i2 = weathericons[we.attributes.forecast[1]['condition']]
|
i2 = weathericons[we.attributes.forecast[1]['condition']]
|
||||||
u2 = we.attributes.forecast[1]['temperature']
|
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}")
|
self.send_mqtt_msg(f"weatherUpdate,?{weathericons[we.state]}?{we.attributes.temperature}{unit}?{26}?{we.attributes.humidity} %?{o1}?{i1}?{u1}?{o2}?{i2}?{u2}")
|
||||||
@@ -554,4 +559,4 @@ class NsPanelLovelaceUI:
|
|||||||
sat = r
|
sat = r
|
||||||
hsv = (math.degrees(math.atan2(y, x))%360/360, sat, 1)
|
hsv = (math.degrees(math.atan2(y, x))%360/360, sat, 1)
|
||||||
rgb = self.hsv2rgb(hsv[0],hsv[1],hsv[2])
|
rgb = self.hsv2rgb(hsv[0],hsv[1],hsv[2])
|
||||||
return rgb
|
return rgb
|
||||||
|
|||||||
Reference in New Issue
Block a user