|
30 | 30 |
|
31 | 31 | import babel.dates |
32 | 32 | from psutil._common import bytes2human |
| 33 | +import requests |
33 | 34 |
|
34 | 35 | import library.config as config |
35 | 36 | from library.display import display |
@@ -767,3 +768,77 @@ def stats(): |
767 | 768 | theme_data = config.THEME_DATA['STATS']['CUSTOM'][custom_stat].get("LINE_GRAPH", None) |
768 | 769 | if theme_data is not None and last_values is not None: |
769 | 770 | display_themed_line_graph(theme_data=theme_data, values=last_values) |
| 771 | + |
| 772 | +class Weather: |
| 773 | + @staticmethod |
| 774 | + def stats(): |
| 775 | + WEATHER_UNITS = {'metric': '°C', 'imperial': '°F', 'standard': '°K'} |
| 776 | + |
| 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: |
| 792 | + if HW_SENSORS in ["STATIC", "STUB"]: |
| 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 |
| 800 | + else: |
| 801 | + # API Parameters |
| 802 | + lat = config.CONFIG_DATA['config'].get('LATITUDE', "") |
| 803 | + lon = config.CONFIG_DATA['config'].get('LONGITUDE', "") |
| 804 | + api_key = config.CONFIG_DATA['config'].get('API_KEY', "") |
| 805 | + units = config.CONFIG_DATA['config'].get('WEATHER_UNITS', "metric") |
| 806 | + lang = config.CONFIG_DATA['config'].get('LANGUAGE', "en") |
| 807 | + deg = WEATHER_UNITS.get(units, '°?') |
| 808 | + if api_key: |
| 809 | + url = f'https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=minutely,hourly,daily,alerts&appid={api_key}&units={units}&lang={lang}' |
| 810 | + try: |
| 811 | + response = requests.get(url) |
| 812 | + if response.status_code == 200: |
| 813 | + try: |
| 814 | + data = response.json() |
| 815 | + temp = f"{data['current']['temp']:.1f}{deg}" |
| 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}%" |
| 821 | + now = datetime.datetime.now() |
| 822 | + time = f"@{now.hour:02d}:{now.minute:02d}" |
| 823 | + except Exception as e: |
| 824 | + logger.error(str(e)) |
| 825 | + desc = "Error fetching weather" |
| 826 | + else: |
| 827 | + print(f"Failed to fetch weather data. Status code: {response.status_code}") |
| 828 | + print(f"Response content: {response.content}") |
| 829 | + logger.error(response.text) |
| 830 | + desc = response.json().get('message') |
| 831 | + except: |
| 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