1414import voluptuous as vol
1515from homeassistant .components .sensor import PLATFORM_SCHEMA
1616from homeassistant .components .weather import ATTR_FORECAST_CONDITION
17- from homeassistant .config_entries import SOURCE_IMPORT
17+ from homeassistant .config_entries import SOURCE_IMPORT , ConfigEntry
1818from homeassistant .const import (
1919 ATTR_ATTRIBUTION ,
2020 ATTR_DEVICE_CLASS ,
@@ -86,22 +86,51 @@ async def async_setup_platform(
8686
8787def fix_kinds (kinds : List [str ], warn = True ) -> List [str ]:
8888 """Remove unwanted values from kinds."""
89- if "forecast" in kinds :
90- kinds .remove ("forecast" )
89+ kinds = set (kinds )
9190
92- if "weather" in kinds :
93- if warn :
94- _LOGGER .warning (
95- 'Deprecated condition "weather". Please replace it to "condition"'
96- )
97- kinds = set (kinds )
98- kinds .remove ("weather" )
99- kinds = list (kinds | {"condition" })
91+ for k in ["forecast" , "pressure_mmhg" , "weather" ]:
92+ if k in kinds :
93+ kinds .remove (k )
94+
95+ if k == "weather" :
96+ kinds = kinds | {"condition" }
97+ if warn :
98+ _LOGGER .warning (
99+ 'Deprecated condition "weather". Please replace it to "condition"'
100+ )
100101
102+ kinds = list (kinds )
103+ kinds .sort ()
101104 return kinds
102105
103106
104- async def async_setup_entry (hass : HomeAssistant , config_entry , async_add_entities ):
107+ def _gen_entities (
108+ location_name : str ,
109+ coordinator : GismeteoDataUpdateCoordinator ,
110+ config : dict ,
111+ warn : bool ,
112+ ):
113+ """Generate entities."""
114+ entities = []
115+
116+ for k in fix_kinds (
117+ config .get (CONF_MONITORED_CONDITIONS , SENSOR_TYPES .keys ()),
118+ warn = warn ,
119+ ):
120+ entities .append (GismeteoSensor (location_name , k , coordinator ))
121+ if k == "pressure" :
122+ entities .append (GismeteoSensor (location_name , "pressure_mmhg" , coordinator ))
123+
124+ if config .get (CONF_FORECAST , False ):
125+ SENSOR_TYPES ["forecast" ] = FORECAST_SENSOR_TYPE
126+ entities .append (GismeteoSensor (location_name , "forecast" , coordinator ))
127+
128+ return entities
129+
130+
131+ async def async_setup_entry (
132+ hass : HomeAssistant , config_entry : ConfigEntry , async_add_entities
133+ ):
105134 """Add Gismeteo sensor entities."""
106135 entities = []
107136 if config_entry .source == SOURCE_IMPORT :
@@ -110,33 +139,34 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
110139 if cfg [CONF_PLATFORM ] != SENSOR :
111140 continue # pragma: no cover
112141
113- name = cfg [CONF_NAME ]
142+ location_name = cfg [CONF_NAME ]
114143 coordinator = hass .data [DOMAIN ][uid ][COORDINATOR ]
115144
116- for kind in fix_kinds (cfg [CONF_MONITORED_CONDITIONS ]):
117- entities .append (GismeteoSensor (name , kind , coordinator ))
118-
119- if cfg .get (CONF_FORECAST , True ):
120- SENSOR_TYPES ["forecast" ] = FORECAST_SENSOR_TYPE
121- entities .append (GismeteoSensor (name , "forecast" , coordinator ))
145+ entities .extend (
146+ _gen_entities (
147+ location_name ,
148+ coordinator ,
149+ cfg ,
150+ True ,
151+ )
152+ )
122153
123154 else :
124155 # Setup from config entry
125156 config = config_entry .data .copy () # type: dict
126157 config .update (config_entry .options )
127158
128- name = config [CONF_NAME ]
159+ location_name = config [CONF_NAME ]
129160 coordinator = hass .data [DOMAIN ][config_entry .entry_id ][COORDINATOR ]
130161
131- for kind in fix_kinds (
132- config .get (CONF_MONITORED_CONDITIONS , SENSOR_TYPES .keys ()),
133- warn = False ,
134- ):
135- entities .append (GismeteoSensor (name , kind , coordinator ))
136-
137- if config .get (CONF_FORECAST , True ):
138- SENSOR_TYPES ["forecast" ] = FORECAST_SENSOR_TYPE
139- entities .append (GismeteoSensor (name , "forecast" , coordinator ))
162+ entities .extend (
163+ _gen_entities (
164+ location_name ,
165+ coordinator ,
166+ config ,
167+ False ,
168+ )
169+ )
140170
141171 async_add_entities (entities , False )
142172
@@ -146,48 +176,51 @@ class GismeteoSensor(GismeteoEntity):
146176
147177 def __init__ (
148178 self ,
149- name : str ,
179+ location_name : str ,
150180 kind : str ,
151181 coordinator : GismeteoDataUpdateCoordinator ,
152182 ):
153183 """Initialize the sensor."""
154- super ().__init__ (name , coordinator )
155- self .kind = kind
184+ super ().__init__ (location_name , coordinator )
185+ self ._kind = kind
186+ self ._unit_of_measurement = SENSOR_TYPES [self ._kind ][ATTR_UNIT_OF_MEASUREMENT ]
187+
156188 self ._state = None
157- self ._unit_of_measurement = SENSOR_TYPES [self .kind ][ATTR_UNIT_OF_MEASUREMENT ]
158189
159190 @property
160191 def unique_id (self ):
161192 """Return a unique_id for this entity."""
162- return f"{ self ._gismeteo .unique_id } -{ self .kind } " .lower ()
193+ return f"{ self ._gismeteo .unique_id } -{ self ._kind } " .lower ()
163194
164195 @property
165196 def name (self ):
166197 """Return the name of the sensor."""
167- return f"{ self ._name } { SENSOR_TYPES [self .kind ][ATTR_NAME ]} "
198+ return f"{ self ._location_name } { SENSOR_TYPES [self ._kind ][ATTR_NAME ]} "
168199
169200 @property
170201 def state (self ):
171202 """Return the state."""
172203 data = self ._gismeteo .current
173204 try :
174- if self .kind == "condition" :
205+ if self ._kind == "condition" :
175206 self ._state = self ._gismeteo .condition ()
176- elif self .kind == "forecast" :
207+ elif self ._kind == "forecast" :
177208 self ._state = self ._gismeteo .forecast ()[0 ][ATTR_FORECAST_CONDITION ]
178- elif self .kind == "temperature" :
209+ elif self ._kind == "temperature" :
179210 self ._state = self ._gismeteo .temperature ()
180- elif self .kind == "wind_speed" :
211+ elif self ._kind == "wind_speed" :
181212 self ._state = self ._gismeteo .wind_speed_ms ()
182- elif self .kind == "wind_bearing" :
213+ elif self ._kind == "wind_bearing" :
183214 self ._state = self ._gismeteo .wind_bearing ()
184- elif self .kind == "humidity" :
215+ elif self ._kind == "humidity" :
185216 self ._state = self ._gismeteo .humidity ()
186- elif self .kind == "pressure" :
217+ elif self ._kind == "pressure" :
187218 self ._state = self ._gismeteo .pressure_hpa ()
188- elif self .kind == "clouds" :
219+ elif self ._kind == "pressure_mmhg" :
220+ self ._state = self ._gismeteo .pressure_mmhg ()
221+ elif self ._kind == "clouds" :
189222 self ._state = int (data .get (ATTR_WEATHER_CLOUDINESS ) * 33.33 )
190- elif self .kind == "rain" :
223+ elif self ._kind == "rain" :
191224 self ._state = (
192225 (
193226 data .get (ATTR_WEATHER_PRECIPITATION_AMOUNT )
@@ -198,10 +231,7 @@ def state(self):
198231 if data .get (ATTR_WEATHER_PRECIPITATION_TYPE ) in [1 , 3 ]
199232 else 0
200233 )
201- self ._unit_of_measurement = SENSOR_TYPES [self .kind ][
202- ATTR_UNIT_OF_MEASUREMENT
203- ]
204- elif self .kind == "snow" :
234+ elif self ._kind == "snow" :
205235 self ._state = (
206236 (
207237 data .get (ATTR_WEATHER_PRECIPITATION_AMOUNT )
@@ -212,18 +242,15 @@ def state(self):
212242 if data .get (ATTR_WEATHER_PRECIPITATION_TYPE ) in [2 , 3 ]
213243 else 0
214244 )
215- self ._unit_of_measurement = SENSOR_TYPES [self .kind ][
216- ATTR_UNIT_OF_MEASUREMENT
217- ]
218- elif self .kind == "storm" :
245+ elif self ._kind == "storm" :
219246 self ._state = data .get (ATTR_WEATHER_STORM )
220- elif self .kind == "geomagnetic" :
247+ elif self ._kind == "geomagnetic" :
221248 self ._state = data .get (ATTR_WEATHER_GEOMAGNETIC_FIELD )
222- elif self .kind == "water_temperature" :
249+ elif self ._kind == "water_temperature" :
223250 self ._state = self ._gismeteo .water_temperature ()
224251 except KeyError : # pragma: no cover
225252 self ._state = None
226- _LOGGER .warning ("Condition is currently not available: %s" , self .kind )
253+ _LOGGER .warning ("Condition is currently not available: %s" , self ._kind )
227254
228255 return self ._state
229256
@@ -235,12 +262,12 @@ def unit_of_measurement(self) -> Optional[str]:
235262 @property
236263 def icon (self ) -> Optional [str ]:
237264 """Return the icon to use in the frontend, if any."""
238- return SENSOR_TYPES [self .kind ][ATTR_ICON ]
265+ return SENSOR_TYPES [self ._kind ][ATTR_ICON ]
239266
240267 @property
241268 def device_class (self ) -> Optional [str ]:
242269 """Return the device_class."""
243- return SENSOR_TYPES [self .kind ][ATTR_DEVICE_CLASS ]
270+ return SENSOR_TYPES [self ._kind ][ATTR_DEVICE_CLASS ]
244271
245272 @property
246273 def device_state_attributes (self ) -> Optional [Dict [str , Any ]]:
0 commit comments