1
1
import logging
2
2
3
+ from datetime import datetime
4
+
3
5
from homeassistant .components .weather import WeatherEntity
4
- from homeassistant .const import UnitOfTemperature
6
+ from homeassistant .const import UnitOfTemperature , UnitOfSpeed , UnitOfPrecipitationDepth , UnitOfPressure
5
7
from homeassistant .helpers .update_coordinator import (
6
8
CoordinatorEntity ,
7
9
)
14
16
15
17
async def async_setup_platform (hass , config , async_add_entities , discovery_info = None ):
16
18
_LOGGER .debug (f"IRM KMI setup. Config: { config } " )
17
- coordinator = IrmKmiCoordinator (hass , city_id = config .get ("city_id" ))
19
+ coordinator = IrmKmiCoordinator (hass , coord = {'lat' : config .get ("lat" ), 'long' : config .get ("lon" )})
20
+
18
21
await coordinator .async_request_refresh ()
19
22
20
23
async_add_entities ([IrmKmiWeather (
@@ -29,6 +32,14 @@ def __init__(self, coordinator: IrmKmiCoordinator, name: str) -> None:
29
32
super ().__init__ (coordinator )
30
33
self ._name = name
31
34
35
+ def _current_hour_data (self ) -> dict | None :
36
+ data = self .coordinator .data .get ('for' , {}).get ('hourly' )
37
+ if data is None or not isinstance (data , list ) or len (data ) == 0 :
38
+ return None
39
+ data = data [0 ]
40
+ if datetime .now ().strftime ('%H' ) != data ['hour' ]:
41
+ return None
42
+ return data
32
43
@property
33
44
def name (self ) -> str :
34
45
return self ._name
@@ -44,5 +55,49 @@ def native_temperature(self) -> float | None:
44
55
return self .coordinator .data .get ('obs' , {}).get ('temp' )
45
56
46
57
@property
47
- def native_temperature_unit (self ) -> str :
58
+ def native_temperature_unit (self ) -> str | None :
48
59
return UnitOfTemperature .CELSIUS
60
+
61
+ @property
62
+ def native_wind_speed_unit (self ) -> str | None :
63
+ return UnitOfSpeed .KILOMETERS_PER_HOUR
64
+
65
+ @property
66
+ def native_wind_speed (self ) -> float | None :
67
+ data = self ._current_hour_data ()
68
+ return data .get ('windSpeedKm' , None )
69
+
70
+ @property
71
+ def native_wind_gust_speed (self ) -> float | None :
72
+ data = self ._current_hour_data ()
73
+ return data .get ('windPeakSpeedKm' , None )
74
+
75
+ @property
76
+ def wind_bearing (self ) -> float | str | None :
77
+ data = self ._current_hour_data ()
78
+ return data .get ('windDirection' , None )
79
+
80
+ @property
81
+ def native_precipitation_unit (self ) -> str | None :
82
+ return UnitOfPrecipitationDepth .MILLIMETERS
83
+
84
+ @property
85
+ def native_pressure (self ) -> float | None :
86
+ data = self ._current_hour_data ()
87
+ return data .get ('pressure' , None )
88
+
89
+ @property
90
+ def native_pressure_unit (self ) -> str | None :
91
+ return UnitOfPressure .HPA
92
+
93
+ @property
94
+ def uv_index (self ) -> float | None :
95
+ data = self .coordinator .data .get ('module' , None )
96
+ if data is None or not isinstance (data , list ):
97
+ return None
98
+
99
+ for module in data :
100
+ if module .get ('type' , None ) == 'uv' :
101
+ return module .get ('data' , {}).get ('levelValue' )
102
+
103
+ return None
0 commit comments