@@ -772,18 +772,37 @@ def stats():
772772class Weather :
773773 @staticmethod
774774 def stats ():
775- weather_text = None
776- TEXT = config .THEME_DATA ['STATS' ].get ('WEATHER' , {}).get ('TEXT' , {})
777775 WEATHER_UNITS = {'metric' : '°C' , 'imperial' : '°F' , 'standard' : '°K' }
778776
779- if TEXT .get ("SHOW" ):
777+ weather_theme_data = config .THEME_DATA ['STATS' ].get ('WEATHER' , {})
778+ wtemperature_theme_data = weather_theme_data .get ('TEMPERATURE' , {}).get ('TEXT' , {})
779+ wfelt_theme_data = weather_theme_data .get ('TEMPERATURE_FELT' , {}).get ('TEXT' , {})
780+ wupdatetime_theme_data = weather_theme_data .get ('UPDATE_TIME' , {}).get ('TEXT' , {})
781+ wdescription_theme_data = weather_theme_data .get ('WEATHER_DESCRIPTION' , {}).get ('TEXT' , {})
782+ whumidity_theme_data = weather_theme_data .get ('HUMIDITY' , {}).get ('TEXT' , {})
783+
784+ # Retrieve information used to center description, if needed
785+ center_description_length = 40
786+ if 'CENTER_LENGTH' in wdescription_theme_data :
787+ center_description_length = wdescription_theme_data ['CENTER_LENGTH' ]
788+
789+ activate = True if wtemperature_theme_data .get ("SHOW" ) or wfelt_theme_data .get ("SHOW" ) or wupdatetime_theme_data .get ("SHOW" ) or wdescription_theme_data .get ("SHOW" ) or whumidity_theme_data .get ("SHOW" ) else False
790+
791+ if activate :
780792 if HW_SENSORS in ["STATIC" , "STUB" ]:
781- weather_text = "+1°C (-1°C) @21:40 Clouds "
793+ temp = "17.5°C"
794+ feel = "(17.2°C)"
795+ desc = "Cloudy"
796+ time = "@15:33"
797+ humidity = "45%"
798+ if wdescription_theme_data ['CENTER_LENGTH' ]:
799+ desc = "x" * center_description_length
782800 else :
801+ # API Parameters
783802 lat = config .CONFIG_DATA ['config' ].get ('LATITUDE' , "" )
784803 lon = config .CONFIG_DATA ['config' ].get ('LONGITUDE' , "" )
785804 api_key = config .CONFIG_DATA ['config' ].get ('API_KEY' , "" )
786- units = config .CONFIG_DATA ['config' ].get ('WEATHER_UNITS' , "" )
805+ units = config .CONFIG_DATA ['config' ].get ('WEATHER_UNITS' , "metric " )
787806 lang = config .CONFIG_DATA ['config' ].get ('LANGUAGE' , "en" )
788807 deg = WEATHER_UNITS .get (units , '°?' )
789808 if api_key :
@@ -793,23 +812,33 @@ def stats():
793812 if response .status_code == 200 :
794813 try :
795814 data = response .json ()
796- # print(data)
797815 temp = f"{ data ['current' ]['temp' ]:.1f} { deg } "
798- feel = f"{ data ['current' ]['feels_like' ]:.1f} { deg } "
799- desc = data ['current' ]['weather' ][0 ]['description' ]
816+ feel = f"({ data ['current' ]['feels_like' ]:.1f} { deg } )"
817+ desc = data ['current' ]['weather' ][0 ]['description' ].capitalize ()
818+ if wdescription_theme_data ['CENTER_LENGTH' ]:
819+ desc = desc .center (center_description_length )
820+ humidity = f"{ data ['current' ]['humidity' ]:.0f} %"
800821 now = datetime .datetime .now ()
801822 time = f"@{ now .hour :02d} :{ now .minute :02d} "
802- weather_text = f"{ temp } ({ feel } ) { time } { desc .capitalize ()} "
803823 except Exception as e :
804824 logger .error (str (e ))
805- weather_text = "Error fetching weather"
825+ desc = "Error fetching weather"
806826 else :
807827 print (f"Failed to fetch weather data. Status code: { response .status_code } " )
808828 print (f"Response content: { response .content } " )
809829 logger .error (response .text )
810- weather_text = response .json ().get ('message' )
830+ desc = response .json ().get ('message' )
811831 except :
812- weather_text = "Connection error"
813-
814- if TEXT and TEXT .get ("SHOW" ) and weather_text :
815- display_themed_value (theme_data = TEXT , value = weather_text )
832+ desc = "Connection error"
833+
834+ if activate :
835+ # Display Temperature
836+ display_themed_value (theme_data = wtemperature_theme_data , value = temp )
837+ # Display Temperature Felt
838+ display_themed_value (theme_data = wfelt_theme_data , value = feel )
839+ # Display Update Time
840+ display_themed_value (theme_data = wupdatetime_theme_data , value = time )
841+ # Display Humidity
842+ display_themed_value (theme_data = whumidity_theme_data , value = humidity )
843+ # Display Weather Description
844+ display_themed_value (theme_data = wdescription_theme_data , value = desc )
0 commit comments