chutney/weather_display.py

48 lines
1.7 KiB
Python
Raw Normal View History

2022-12-24 10:59:04 -05:00
import colors
import requests
from configparser import ConfigParser
2022-12-24 12:10:46 -05:00
2022-12-24 10:59:04 -05:00
class WeatherDisplay:
def __init__(self, characterDisplay, topRow, configFile):
self.characterDisplay = characterDisplay
self.topRow = topRow
self.currentTemperature = [9, 9]
self.currentColor = colors.BLACK
self.color = colors.BLUE
2022-12-24 12:10:46 -05:00
2022-12-24 10:59:04 -05:00
config = ConfigParser()
config.read(configFile)
self.host = config['weather'].get('host')
self.user = config['weather'].get('user')
self.password = config['weather'].get('pass')
self.lat = config['weather'].get('lat')
self.lon = config['weather'].get('lon')
print(self.host)
def showWeather(self):
2022-12-24 12:10:46 -05:00
self.characterDisplay.clearRow(self.topRow)
self.showTemperature(
self.getTemperatureCharacters(self.getTemperature()))
2022-12-24 10:59:04 -05:00
def showTemperature(self, temperature):
2022-12-24 12:10:46 -05:00
start = 13 if ( len(temperature) == 2 and temperature[0] == '-') or len(temperature) == 1 else 9
2022-12-24 10:59:04 -05:00
2022-12-24 12:10:46 -05:00
for c in temperature:
if c == '-':
self.characterDisplay.displayNegative(
start-3, self.topRow, self.color)
else:
self.characterDisplay.displayDigit(
start, self.topRow, c, self.color)
start = start + 4
2022-12-24 10:59:04 -05:00
def getTemperature(self):
2022-12-24 12:10:46 -05:00
return list(filter(lambda t: t["timestep"] == 'current', requests.get(f'https://{self.host}/api/weather?lat={self.lat}&lon={self.lon}&units=metric', auth=(self.user, self.password)).json()["tomorrow"]["data"]['timelines']))[0]["intervals"][0]["values"]["temperature"]
2022-12-24 10:59:04 -05:00
def getTemperatureCharacters(self, temperature):
2022-12-24 12:10:46 -05:00
return list(str(round(temperature)))