Merge pull request #40 from gerard33/patch-1

Add localization for weather forecast
This commit is contained in:
joBr99
2022-03-17 21:54:02 +01:00
committed by GitHub
4 changed files with 18 additions and 10 deletions

View File

@@ -260,6 +260,7 @@ nspanel-1:
value: 0
locale: "de_DE" # only used if babel python package is installed
dateFormatBabel: "full" # only used if babel python package is installed
# formatting options on https://babel.pocoo.org/en/latest/dates.html?highlight=name%20of%20day#date-fields
timeFormat: "%H:%M"
dateFormat: "%A, %d. %B %Y" # ignored if babel python package is installed
weatherEntity: weather.example

View File

@@ -14,6 +14,7 @@ nspanel:
value: 0
locale: "de_DE" # only used if babel python package is installed
dateFormatBabel: "full" # only used if babel python package is installed
# formatting options on https://babel.pocoo.org/en/latest/dates.html?highlight=name%20of%20day#date-fields
timeFormat: "%H:%M"
dateFormat: "%A, %d. %B %Y" # ignored if babel python package is installed
weatherEntity: weather.example

View File

@@ -4,6 +4,13 @@ import hassapi as hass
import math
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):
def initialize(self):
@@ -20,6 +27,8 @@ class NsPanelLovelaceUI:
# check configured items
self.check_items()
self.babel_spec = importlib.util.find_spec("babel")
# Setup, mqtt subscription and callback
self.mqtt = self.api.get_plugin_api("MQTT")
self.mqtt.mqtt_subscribe(topic=self.config["panelRecvTopic"])
@@ -140,12 +149,8 @@ class NsPanelLovelaceUI:
self.send_mqtt_msg("time,{0}".format(time))
def update_date(self, kwargs):
import importlib
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 self.babel_spec is not None:
self.api.log("Babel package found", level="DEBUG")
if "dateFormatBabel" in self.config:
dateformat = self.config["dateFormatBabel"]
else:
@@ -153,7 +158,7 @@ class NsPanelLovelaceUI:
date = babel.dates.format_date(datetime.datetime.now(), dateformat, locale=self.config["locale"])
self.send_mqtt_msg(f"date,?{date}")
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"])
self.send_mqtt_msg(f"date,?{date}")
@@ -187,12 +192,12 @@ class NsPanelLovelaceUI:
o1 = we.attributes.forecast[0]['datetime']
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']]
u1 = we.attributes.forecast[0]['temperature']
o2 = we.attributes.forecast[1]['datetime']
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']]
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}")
@@ -554,4 +559,4 @@ class NsPanelLovelaceUI:
sat = r
hsv = (math.degrees(math.atan2(y, x))%360/360, sat, 1)
rgb = self.hsv2rgb(hsv[0],hsv[1],hsv[2])
return rgb
return rgb

View File

@@ -20,6 +20,7 @@ nspanel-1:
value: 0
locale: "de_DE" # only used if babel python package is installed
dateFormatBabel: "full" # only used if babel python package is installed
# formatting options on https://babel.pocoo.org/en/latest/dates.html?highlight=name%20of%20day#date-fields
timeFormat: "%H:%M"
dateFormat: "%A, %d. %B %Y" # ignored if babel python package is installed
weatherEntity: weather.example