13
13
WeatherEntity ,
14
14
)
15
15
from homeassistant .const import (
16
+ CONF_NAME ,
16
17
TEMP_CELSIUS ,
17
18
)
18
19
@@ -29,11 +30,14 @@ async def async_setup_entry(hass, entry, async_add_devices):
29
30
class KnmiWeather (KnmiEntity , WeatherEntity ):
30
31
"""knmi Weather class."""
31
32
33
+ def __init__ (self , coordinator , config_entry ):
34
+ super ().__init__ (coordinator , config_entry )
35
+ self .entry_name = config_entry .data .get (CONF_NAME )
36
+
32
37
@property
33
38
def name (self ):
34
39
"""Return the name of the sensor."""
35
- location = self .coordinator .data ["plaats" ]
36
- return f"{ DEFAULT_NAME } { location } "
40
+ return f"{ DEFAULT_NAME } { self .entry_name } "
37
41
38
42
@property
39
43
def state (self ):
@@ -43,12 +47,14 @@ def state(self):
43
47
@property
44
48
def condition (self ):
45
49
"""Return the current condition."""
46
- return CONDITIONS_MAP [self .coordinator .data ["d0weer" ]]
50
+ if super ().getData ("d0weer" ) is not None :
51
+ return CONDITIONS_MAP [super ().getData ("d0weer" )]
47
52
48
53
@property
49
54
def temperature (self ):
50
55
"""Return the temperature."""
51
- return float (self .coordinator .data ["temp" ])
56
+ if super ().getData ("temp" ) is not None :
57
+ return float (super ().getData ("temp" ))
52
58
53
59
@property
54
60
def temperature_unit (self ):
@@ -58,27 +64,32 @@ def temperature_unit(self):
58
64
@property
59
65
def pressure (self ):
60
66
"""Return the pressure."""
61
- return float (self .coordinator .data ["luchtd" ])
67
+ if super ().getData ("luchtd" ) is not None :
68
+ return float (super ().getData ("luchtd" ))
62
69
63
70
@property
64
71
def humidity (self ):
65
72
"""Return the humidity."""
66
- return float (self .coordinator .data ["lv" ])
73
+ if super ().getData ("lv" ) is not None :
74
+ return float (super ().getData ("lv" ))
67
75
68
76
@property
69
77
def wind_speed (self ):
70
78
"""Return the wind speed."""
71
- return float (self .coordinator .data ["windkmh" ])
79
+ if super ().getData ("windkmh" ) is not None :
80
+ return float (super ().getData ("windkmh" ))
72
81
73
82
@property
74
83
def wind_bearing (self ):
75
84
"""Return the wind direction."""
76
- return WIND_DIRECTION_MAP [self .coordinator .data ["windr" ]]
85
+ if super ().getData ("windr" ) is not None :
86
+ return WIND_DIRECTION_MAP [super ().getData ("windr" )]
77
87
78
88
@property
79
89
def visibility (self ):
80
90
"""Return the wind direction."""
81
- return float (self .coordinator .data ["zicht" ]) / 10
91
+ if super ().getData ("zicht" ) is not None :
92
+ return float (super ().getData ("zicht" )) / 10
82
93
83
94
@property
84
95
def forecast (self ):
@@ -88,21 +99,45 @@ def forecast(self):
88
99
89
100
for i in range (0 , 3 ):
90
101
date = today + timedelta (days = i )
91
- nextDay = {
102
+ condition = (
103
+ CONDITIONS_MAP [super ().getData (f"d{ i } weer" )]
104
+ if super ().getData (f"d{ i } weer" ) is not None
105
+ else None
106
+ )
107
+ wind_bearing = (
108
+ WIND_DIRECTION_MAP [super ().getData (f"d{ i } windr" )]
109
+ if super ().getData (f"d{ i } windr" ) is not None
110
+ else None
111
+ )
112
+ temp_low = (
113
+ float (super ().getData (f"d{ i } tmin" ))
114
+ if super ().getData (f"d{ i } tmin" ) is not None
115
+ else None
116
+ )
117
+ temp = (
118
+ float (super ().getData (f"d{ i } tmin" ))
119
+ if super ().getData (f"d{ i } tmin" ) is not None
120
+ else None
121
+ )
122
+ precipitation = (
123
+ float (super ().getData (f"d{ i } neerslag" ))
124
+ if super ().getData (f"d{ i } neerslag" ) is not None
125
+ else None
126
+ )
127
+ wind_speed = (
128
+ float (super ().getData (f"d{ i } windkmh" ))
129
+ if super ().getData (f"d{ i } windkmh" ) is not None
130
+ else None
131
+ )
132
+ next_day = {
92
133
ATTR_FORECAST_TIME : date .isoformat (),
93
- ATTR_FORECAST_CONDITION : CONDITIONS_MAP [
94
- self .coordinator .data [f"d{ i } weer" ]
95
- ],
96
- ATTR_FORECAST_TEMP_LOW : float (self .coordinator .data [f"d{ i } tmin" ]),
97
- ATTR_FORECAST_TEMP : float (self .coordinator .data [f"d{ i } tmax" ]),
98
- ATTR_FORECAST_PRECIPITATION : float (
99
- self .coordinator .data [f"d{ i } neerslag" ]
100
- ),
101
- ATTR_FORECAST_WIND_BEARING : WIND_DIRECTION_MAP [
102
- self .coordinator .data [f"d{ i } windr" ]
103
- ],
104
- ATTR_FORECAST_WIND_SPEED : float (self .coordinator .data [f"d{ i } windkmh" ]),
134
+ ATTR_FORECAST_CONDITION : condition ,
135
+ ATTR_FORECAST_TEMP_LOW : temp_low ,
136
+ ATTR_FORECAST_TEMP : temp ,
137
+ ATTR_FORECAST_PRECIPITATION : precipitation ,
138
+ ATTR_FORECAST_WIND_BEARING : wind_bearing ,
139
+ ATTR_FORECAST_WIND_SPEED : wind_speed ,
105
140
}
106
- forecast .append (nextDay )
141
+ forecast .append (next_day )
107
142
108
143
return forecast
0 commit comments