75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
import json
|
|
|
|
from smhi.smhi_lib import Smhi
|
|
|
|
from ..homeassistant import HomeAssistant
|
|
|
|
|
|
class Plugin:
|
|
def __init__(self, config: dict) -> None:
|
|
self.config = config
|
|
self.homeassistant = HomeAssistant(config)
|
|
self.station = Smhi(
|
|
self.homeassistant.ha_config["longitude"], self.homeassistant.ha_config["latitude"]
|
|
)
|
|
self.weather_conditions = {
|
|
1: "Clear sky",
|
|
2: "Nearly clear sky",
|
|
3: "Variable cloudiness",
|
|
4: "Halfclear sky",
|
|
5: "Cloudy sky",
|
|
6: "Overcast",
|
|
7: "Fog",
|
|
8: "Light rain showers",
|
|
9: "Moderate rain showers",
|
|
10: "Heavy rain showers",
|
|
11: "Thunderstorm",
|
|
12: "Light sleet showers",
|
|
13: "Moderate sleet showers",
|
|
14: "Heavy sleet showers",
|
|
15: "Light snow showers",
|
|
16: "Moderate snow showers",
|
|
17: "Heavy snow showers",
|
|
18: "Light rain",
|
|
19: "Moderate rain",
|
|
20: "Heavy rain",
|
|
21: "Thunder",
|
|
22: "Light sleet",
|
|
23: "Moderate sleet",
|
|
24: "Heavy sleet",
|
|
25: "Light snowfall",
|
|
26: "Moderate snowfall",
|
|
27: "Heavy snowfall",
|
|
}
|
|
|
|
def prompt(self):
|
|
return None
|
|
|
|
def tools(self):
|
|
tools = [
|
|
{
|
|
"type": "function",
|
|
"function": {
|
|
"name": "get_weather_forecast",
|
|
"function_to_call": self.get_weather_forecast,
|
|
"description": "Get weather forecast for the following 24 hours.",
|
|
},
|
|
}
|
|
]
|
|
return tools
|
|
|
|
def get_weather_forecast(self, hours=24):
|
|
forecast = []
|
|
for hour in self.station.get_forecast_hour()[:hours]:
|
|
forecast.append(
|
|
{
|
|
"time": hour.valid_time.strftime("%Y-%m-%d %H:%M"),
|
|
"weather": self.weather_conditions[hour.symbol],
|
|
"temperature": hour.temperature,
|
|
# "cloudiness": hour.cloudiness,
|
|
"total_precipitation": hour.total_precipitation,
|
|
"wind_speed": hour.wind_speed,
|
|
}
|
|
)
|
|
return json.dumps(forecast)
|