Add "UV Index" to IPMA (#100383)

* Bump pyipma to 3.0.7

* Add uv index sensor to IPMA
This commit is contained in:
Diogo Morgado 2023-09-27 16:45:21 +01:00 committed by GitHub
parent 5541181969
commit 4066f657d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 11 deletions

View File

@ -6,5 +6,5 @@
"documentation": "https://www.home-assistant.io/integrations/ipma",
"iot_class": "cloud_polling",
"loggers": ["geopy", "pyipma"],
"requirements": ["pyipma==3.0.6"]
"requirements": ["pyipma==3.0.7"]
}

View File

@ -8,6 +8,8 @@ import logging
from pyipma.api import IPMA_API
from pyipma.location import Location
from pyipma.rcm import RCM
from pyipma.uv import UV
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
@ -33,19 +35,32 @@ class IPMASensorEntityDescription(SensorEntityDescription, IPMARequiredKeysMixin
"""Describes IPMA sensor entity."""
async def async_retrive_rcm(location: Location, api: IPMA_API) -> int | None:
async def async_retrieve_rcm(location: Location, api: IPMA_API) -> int | None:
"""Retrieve RCM."""
fire_risk = await location.fire_risk(api)
fire_risk: RCM = await location.fire_risk(api)
if fire_risk:
return fire_risk.rcm
return None
async def async_retrieve_uvi(location: Location, api: IPMA_API) -> int | None:
"""Retrieve UV."""
uv_risk: UV = await location.uv_risk(api)
if uv_risk:
return round(uv_risk.iUv)
return None
SENSOR_TYPES: tuple[IPMASensorEntityDescription, ...] = (
IPMASensorEntityDescription(
key="rcm",
translation_key="fire_risk",
value_fn=async_retrive_rcm,
value_fn=async_retrieve_rcm,
),
IPMASensorEntityDescription(
key="uvi",
translation_key="uv_index",
value_fn=async_retrieve_uvi,
),
)
@ -81,7 +96,7 @@ class IPMASensor(SensorEntity, IPMADevice):
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self) -> None:
"""Update Fire risk."""
"""Update sensors."""
async with asyncio.timeout(10):
self._attr_native_value = await self.entity_description.value_fn(
self._location, self._api

View File

@ -28,6 +28,9 @@
"sensor": {
"fire_risk": {
"name": "Fire risk"
},
"uv_index": {
"name": "UV index"
}
}
}

View File

@ -1764,7 +1764,7 @@ pyinsteon==1.5.1
pyintesishome==1.8.0
# homeassistant.components.ipma
pyipma==3.0.6
pyipma==3.0.7
# homeassistant.components.ipp
pyipp==0.14.4

View File

@ -1325,7 +1325,7 @@ pyicloud==1.0.0
pyinsteon==1.5.1
# homeassistant.components.ipma
pyipma==3.0.6
pyipma==3.0.7
# homeassistant.components.ipp
pyipp==0.14.4

View File

@ -27,6 +27,14 @@ class MockLocation:
)
return RCM("some place", 3, (0, 0))
async def uv_risk(self, api):
"""Mock UV Index."""
UV = namedtuple(
"UV",
["idPeriodo", "intervaloHora", "data", "globalIdLocal", "iUv"],
)
return UV(0, "0", datetime.now(), 0, 5.7)
async def observation(self, api):
"""Mock Observation."""
Observation = namedtuple(

View File

@ -10,10 +10,7 @@ from tests.common import MockConfigEntry
async def test_ipma_fire_risk_create_sensors(hass):
"""Test creation of fire risk sensors."""
with patch(
"pyipma.location.Location.get",
return_value=MockLocation(),
):
with patch("pyipma.location.Location.get", return_value=MockLocation()):
entry = MockConfigEntry(domain="ipma", data=ENTRY_CONFIG)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
@ -22,3 +19,17 @@ async def test_ipma_fire_risk_create_sensors(hass):
state = hass.states.get("sensor.hometown_fire_risk")
assert state.state == "3"
async def test_ipma_uv_index_create_sensors(hass):
"""Test creation of uv index sensors."""
with patch("pyipma.location.Location.get", return_value=MockLocation()):
entry = MockConfigEntry(domain="ipma", data=ENTRY_CONFIG)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.hometown_uv_index")
assert state.state == "6"