Skip to content

Commit 216d862

Browse files
committed
Add pressure mmHg sensor
1 parent 48a65d1 commit 216d862

File tree

7 files changed

+126
-64
lines changed

7 files changed

+126
-64
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ I put a lot of work into making this repo and component available and updated to
155155
>
156156
> **pressure**\
157157
> The sea-level air pressure in millibars.
158+
> At the same time, a second sensor is created, indicating the same pressure in mmHg.
158159
>
159160
> **wind_speed**\
160161
> The wind speed.

custom_components/gismeteo/const.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@
147147
ATTR_NAME: "Pressure",
148148
ATTR_UNIT_OF_MEASUREMENT: PRESSURE_HPA,
149149
},
150+
"pressure_mmhg": {
151+
ATTR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
152+
ATTR_ICON: None,
153+
ATTR_NAME: "Pressure mmHg",
154+
ATTR_UNIT_OF_MEASUREMENT: "mmHg",
155+
},
150156
"wind_speed": {
151157
ATTR_DEVICE_CLASS: None,
152158
ATTR_ICON: "mdi:weather-windy",

custom_components/gismeteo/entity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
class GismeteoEntity(CoordinatorEntity):
2020
"""Gismeteo entity."""
2121

22-
def __init__(self, name: str, coordinator: GismeteoDataUpdateCoordinator):
22+
def __init__(self, location_name: str, coordinator: GismeteoDataUpdateCoordinator):
2323
"""Class initialization."""
2424
super().__init__(coordinator)
25-
self._name = name
25+
self._location_name = location_name
2626

2727
@property
2828
def _gismeteo(self) -> GismeteoApiClient:

custom_components/gismeteo/sensor.py

Lines changed: 84 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import voluptuous as vol
1515
from homeassistant.components.sensor import PLATFORM_SCHEMA
1616
from homeassistant.components.weather import ATTR_FORECAST_CONDITION
17-
from homeassistant.config_entries import SOURCE_IMPORT
17+
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
1818
from homeassistant.const import (
1919
ATTR_ATTRIBUTION,
2020
ATTR_DEVICE_CLASS,
@@ -86,22 +86,51 @@ async def async_setup_platform(
8686

8787
def 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]]:

custom_components/gismeteo/weather.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
103103
class GismeteoWeather(GismeteoEntity, WeatherEntity):
104104
"""Implementation of an Gismeteo sensor."""
105105

106-
def __init__(self, name: str, coordinator: GismeteoDataUpdateCoordinator):
106+
def __init__(self, location_name: str, coordinator: GismeteoDataUpdateCoordinator):
107107
"""Initialize."""
108-
super().__init__(name, coordinator)
108+
super().__init__(location_name, coordinator)
109109
self._attrs = {}
110110

111111
@property
@@ -116,7 +116,7 @@ def unique_id(self):
116116
@property
117117
def name(self):
118118
"""Return the name."""
119-
return self._name
119+
return self._location_name
120120

121121
@property
122122
def attribution(self):

info.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _Component to integrate with Gismeteo weather provider._
2121
- Sensors of current weather:
2222
- air temperature,
2323
- air relative humidity,
24-
- air pressure,
24+
- air pressure (in millibars and mmHg),
2525
- wind bearing and speed,
2626
- cloud coverage,
2727
- rain and snow volume,

tests/test_sensor.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,35 @@
1919
DOMAIN,
2020
SENSOR_TYPES,
2121
)
22-
from custom_components.gismeteo.sensor import GismeteoSensor
22+
from custom_components.gismeteo.sensor import GismeteoSensor, fix_kinds
23+
24+
25+
async def test_fix_kinds(caplog):
26+
"""Test fix_kinds function."""
27+
caplog.clear()
28+
res = fix_kinds([])
29+
assert res == []
30+
assert len(caplog.records) == 0
31+
32+
caplog.clear()
33+
res = fix_kinds(["qwe", "asd"])
34+
assert res == ["asd", "qwe"]
35+
assert len(caplog.records) == 0
36+
37+
caplog.clear()
38+
res = fix_kinds(["qwe", "asd", "pressure", "forecast", "pressure_mmhg"])
39+
assert res == ["asd", "pressure", "qwe"]
40+
assert len(caplog.records) == 0
41+
42+
caplog.clear()
43+
res = fix_kinds(["qwe", "asd", "weather"])
44+
assert res == ["asd", "condition", "qwe"]
45+
assert len(caplog.records) == 1
46+
47+
caplog.clear()
48+
res = fix_kinds(["qwe", "asd", "weather"], False)
49+
assert res == ["asd", "condition", "qwe"]
50+
assert len(caplog.records) == 0
2351

2452

2553
async def test_sensor_initialization(hass: HomeAssistant):

0 commit comments

Comments
 (0)