mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Migrate London Air to the pylondonair library (#188163)
Co-authored-by: Joostlek <joostlek@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Joostlek
Claude Opus 5.5
parent
3d300e1fc5
commit
d5f17d6381
@@ -5,5 +5,6 @@
|
||||
"codeowners": [],
|
||||
"documentation": "https://www.home-assistant.io/integrations/london_air",
|
||||
"iot_class": "cloud_polling",
|
||||
"quality_scale": "legacy"
|
||||
"quality_scale": "legacy",
|
||||
"requirements": ["pylondonair==0.1.1"]
|
||||
}
|
||||
|
||||
@@ -1,21 +1,28 @@
|
||||
"""Sensor for checking the status of London air."""
|
||||
|
||||
from datetime import timedelta
|
||||
from http import HTTPStatus
|
||||
from datetime import datetime, timedelta
|
||||
import logging
|
||||
from typing import Any, override
|
||||
|
||||
import probatio
|
||||
import requests
|
||||
from pylondonair import (
|
||||
NO_SPECIES_DATA,
|
||||
LondonAirClient,
|
||||
LondonAirConnectionError,
|
||||
LondonAirData,
|
||||
LondonAirUpdateError,
|
||||
Site,
|
||||
)
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
|
||||
SensorEntity,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util import Throttle
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -59,8 +66,6 @@ AUTHORITIES = [
|
||||
"Westminster",
|
||||
]
|
||||
|
||||
URL = "http://api.erg.kcl.ac.uk/AirQuality/Hourly/MonitoringIndex/GroupName=London/Json"
|
||||
|
||||
PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
probatio.Optional(CONF_LOCATIONS, default=AUTHORITIES): probatio.All(
|
||||
@@ -70,35 +75,40 @@ PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
|
||||
)
|
||||
|
||||
|
||||
def setup_platform(
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
add_entities: AddEntitiesCallback,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
discovery_info: DiscoveryInfoType | None = None,
|
||||
) -> None:
|
||||
"""Set up the London Air sensor."""
|
||||
data = APIData()
|
||||
data.update()
|
||||
data = APIData(hass)
|
||||
await data.async_update()
|
||||
|
||||
add_entities((AirSensor(name, data) for name in config[CONF_LOCATIONS]), True)
|
||||
async_add_entities((AirSensor(name, data) for name in config[CONF_LOCATIONS]), True)
|
||||
|
||||
|
||||
class APIData:
|
||||
"""Get the latest data for all authorities."""
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
"""Initialize the AirData object."""
|
||||
self.data = None
|
||||
self._client = LondonAirClient(session=async_get_clientsession(hass))
|
||||
self.data: LondonAirData | None = None
|
||||
self._last_update: datetime | None = None
|
||||
|
||||
# Update only once in scan interval.
|
||||
@Throttle(SCAN_INTERVAL)
|
||||
def update(self):
|
||||
"""Get the latest data from TFL."""
|
||||
response = requests.get(URL, timeout=10)
|
||||
if response.status_code != HTTPStatus.OK:
|
||||
_LOGGER.warning("Invalid response from API")
|
||||
else:
|
||||
self.data = parse_api_response(response.json())
|
||||
async def async_update(self) -> None:
|
||||
"""Get the latest data from the API, at most once per scan interval."""
|
||||
now = dt_util.utcnow()
|
||||
if self._last_update is not None and now - self._last_update <= SCAN_INTERVAL:
|
||||
return
|
||||
self._last_update = now
|
||||
try:
|
||||
self.data = await self._client.async_update()
|
||||
except LondonAirConnectionError as err:
|
||||
_LOGGER.warning("Could not connect to London Air API: %s", err)
|
||||
except LondonAirUpdateError as err:
|
||||
_LOGGER.warning("Invalid response from London Air API: %s", err)
|
||||
|
||||
|
||||
class AirSensor(SensorEntity):
|
||||
@@ -106,15 +116,15 @@ class AirSensor(SensorEntity):
|
||||
|
||||
_attr_icon = "mdi:cloud-outline"
|
||||
|
||||
def __init__(self, name, api_data):
|
||||
def __init__(self, name: str, api_data: APIData) -> None:
|
||||
"""Initialize the sensor."""
|
||||
self._attr_name = self._key = name
|
||||
self._api_data = api_data
|
||||
self._site_data = None
|
||||
self._updated = None
|
||||
self._site_data: list[dict[str, Any]] | None = None
|
||||
self._updated: str | None = None
|
||||
|
||||
@property
|
||||
def site_data(self):
|
||||
def site_data(self) -> list[dict[str, Any]] | None:
|
||||
"""Return the dict of sites data."""
|
||||
return self._site_data
|
||||
|
||||
@@ -122,101 +132,50 @@ class AirSensor(SensorEntity):
|
||||
@override
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
"""Return other details about the sensor state."""
|
||||
attrs = {}
|
||||
attrs: dict[str, Any] = {}
|
||||
attrs["updated"] = self._updated
|
||||
attrs["sites"] = len(self._site_data) if self._site_data is not None else 0
|
||||
attrs["data"] = self._site_data
|
||||
return attrs
|
||||
|
||||
def update(self) -> None:
|
||||
async def async_update(self) -> None:
|
||||
"""Update the sensor."""
|
||||
sites_status: list = []
|
||||
self._api_data.update()
|
||||
if self._api_data.data:
|
||||
self._site_data = self._api_data.data[self._key]
|
||||
self._updated = self._site_data[0]["updated"]
|
||||
sites_status.extend(
|
||||
site["pollutants_status"]
|
||||
for site in self._site_data
|
||||
if site["pollutants_status"] != "no_species_data"
|
||||
)
|
||||
|
||||
if sites_status:
|
||||
self._attr_native_value = max(set(sites_status), key=sites_status.count)
|
||||
else:
|
||||
await self._api_data.async_update()
|
||||
data = self._api_data.data
|
||||
if data is None:
|
||||
self._attr_native_value = None
|
||||
return
|
||||
authority = data.authorities.get(self._key)
|
||||
sites = authority.sites if authority is not None else ()
|
||||
self._site_data = [_site_to_dict(site) for site in sites]
|
||||
self._updated = (
|
||||
sites[0].updated.strftime("%Y-%m-%d %H:%M:%S") if sites else None
|
||||
)
|
||||
self._attr_native_value = authority.status if authority is not None else None
|
||||
|
||||
|
||||
def parse_species(species_data):
|
||||
"""Iterate over list of species at each site."""
|
||||
parsed_species_data = []
|
||||
quality_list = []
|
||||
for species in species_data:
|
||||
if species["@AirQualityBand"] != "No data":
|
||||
species_dict = {}
|
||||
species_dict["description"] = species["@SpeciesDescription"]
|
||||
species_dict["code"] = species["@SpeciesCode"]
|
||||
species_dict["quality"] = species["@AirQualityBand"]
|
||||
species_dict["index"] = species["@AirQualityIndex"]
|
||||
species_dict["summary"] = (
|
||||
f"{species_dict['code']} is {species_dict['quality']}"
|
||||
)
|
||||
parsed_species_data.append(species_dict)
|
||||
quality_list.append(species_dict["quality"])
|
||||
return parsed_species_data, quality_list
|
||||
|
||||
|
||||
def parse_site(entry_sites_data):
|
||||
"""Iterate over all sites at an authority."""
|
||||
authority_data = []
|
||||
for site in entry_sites_data:
|
||||
site_data = {}
|
||||
species_data = []
|
||||
|
||||
site_data["updated"] = site["@BulletinDate"]
|
||||
site_data["latitude"] = site["@Latitude"]
|
||||
site_data["longitude"] = site["@Longitude"]
|
||||
site_data["site_code"] = site["@SiteCode"]
|
||||
site_data["site_name"] = site["@SiteName"].split("-")[-1].lstrip()
|
||||
site_data["site_type"] = site["@SiteType"]
|
||||
|
||||
if isinstance(site["Species"], dict):
|
||||
species_data = [site["Species"]]
|
||||
else:
|
||||
species_data = site["Species"]
|
||||
|
||||
parsed_species_data, quality_list = parse_species(species_data)
|
||||
|
||||
if not parsed_species_data:
|
||||
parsed_species_data.append("no_species_data")
|
||||
site_data["pollutants"] = parsed_species_data
|
||||
|
||||
if quality_list:
|
||||
site_data["pollutants_status"] = max(
|
||||
set(quality_list), key=quality_list.count
|
||||
)
|
||||
site_data["number_of_pollutants"] = len(quality_list)
|
||||
else:
|
||||
site_data["pollutants_status"] = "no_species_data"
|
||||
site_data["number_of_pollutants"] = 0
|
||||
|
||||
authority_data.append(site_data)
|
||||
return authority_data
|
||||
|
||||
|
||||
def parse_api_response(response):
|
||||
"""Parse return dict or list of data from API."""
|
||||
data = dict.fromkeys(AUTHORITIES)
|
||||
for authority in AUTHORITIES:
|
||||
for entry in response["HourlyAirQualityIndex"]["LocalAuthority"]:
|
||||
if entry["@LocalAuthorityName"] == authority:
|
||||
entry_sites_data = []
|
||||
if "Site" in entry:
|
||||
if isinstance(entry["Site"], dict):
|
||||
entry_sites_data = [entry["Site"]]
|
||||
else:
|
||||
entry_sites_data = entry["Site"]
|
||||
|
||||
data[authority] = parse_site(entry_sites_data)
|
||||
|
||||
return data
|
||||
def _site_to_dict(site: Site) -> dict[str, Any]:
|
||||
"""Convert a library site into the attribute dict format."""
|
||||
pollutants: list[dict[str, str] | str] = [
|
||||
{
|
||||
"description": pollutant.description,
|
||||
"code": pollutant.code,
|
||||
"quality": pollutant.quality,
|
||||
"index": pollutant.index,
|
||||
"summary": pollutant.summary,
|
||||
}
|
||||
for pollutant in site.pollutants
|
||||
]
|
||||
if not pollutants:
|
||||
pollutants = [NO_SPECIES_DATA]
|
||||
return {
|
||||
"updated": site.updated.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"latitude": site.latitude,
|
||||
"longitude": site.longitude,
|
||||
"site_code": site.site_code,
|
||||
"site_name": site.site_name,
|
||||
"site_type": site.site_type,
|
||||
"pollutants": pollutants,
|
||||
"pollutants_status": site.status,
|
||||
"number_of_pollutants": site.number_of_pollutants,
|
||||
}
|
||||
|
||||
Generated
+3
@@ -2459,6 +2459,9 @@ pylitejet==0.6.3
|
||||
# homeassistant.components.litterrobot
|
||||
pylitterbot==2025.7.0
|
||||
|
||||
# homeassistant.components.london_air
|
||||
pylondonair==0.1.1
|
||||
|
||||
# homeassistant.components.lutron_caseta
|
||||
pylutron-caseta==0.30.1
|
||||
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
"""Fixtures for the London Air tests."""
|
||||
|
||||
from collections.abc import Generator
|
||||
from datetime import datetime
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from pylondonair import Authority, LondonAirData, Pollutant, Site
|
||||
import pytest
|
||||
|
||||
UPDATED = datetime(2017, 8, 3, 3, 0, 0, tzinfo=ZoneInfo("Europe/London"))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def london_air_data() -> LondonAirData:
|
||||
"""Return London air data matching the historical API fixture."""
|
||||
merton = Authority(
|
||||
name="Merton",
|
||||
code="24",
|
||||
centre_latitude="51.415672",
|
||||
centre_longitude="-0.191814",
|
||||
sites=(
|
||||
Site(
|
||||
site_code="ME2",
|
||||
site_name="Merton Road",
|
||||
site_type="Roadside",
|
||||
latitude="51.4161384794862",
|
||||
longitude="-0.192230805042824",
|
||||
updated=UPDATED,
|
||||
pollutants=(
|
||||
Pollutant(
|
||||
code="PM10",
|
||||
description="PM10 Particulate",
|
||||
quality="Low",
|
||||
index="2",
|
||||
summary="PM10 is Low",
|
||||
index_source="Trigger",
|
||||
),
|
||||
),
|
||||
status="Low",
|
||||
number_of_pollutants=1,
|
||||
latitude_wgs84="6695236.54926",
|
||||
longitude_wgs84="-21399.0353321",
|
||||
),
|
||||
Site(
|
||||
site_code="ME9",
|
||||
site_name="Morden Civic Centre 2",
|
||||
site_type="Roadside",
|
||||
latitude="51.40162",
|
||||
longitude="-0.19589212",
|
||||
updated=UPDATED,
|
||||
pollutants=(
|
||||
Pollutant(
|
||||
code="NO2",
|
||||
description="Nitrogen Dioxide",
|
||||
quality="Low",
|
||||
index="1",
|
||||
summary="NO2 is Low",
|
||||
index_source="Measurement",
|
||||
),
|
||||
),
|
||||
status="Low",
|
||||
number_of_pollutants=1,
|
||||
latitude_wgs84="6692543.79001",
|
||||
longitude_wgs84="-21810.7165116",
|
||||
),
|
||||
),
|
||||
status="Low",
|
||||
centre_latitude_wgs84="6695153.285882",
|
||||
centre_longitude_wgs84="-21352.636807",
|
||||
)
|
||||
city_of_london = Authority(
|
||||
name="City of London",
|
||||
code="7",
|
||||
centre_latitude="51.51333",
|
||||
centre_longitude="-0.088947",
|
||||
sites=(),
|
||||
status=None,
|
||||
centre_latitude_wgs84="6712603.132989",
|
||||
centre_longitude_wgs84="-9901.534748",
|
||||
)
|
||||
barnet = Authority(
|
||||
name="Barnet",
|
||||
code="3",
|
||||
centre_latitude="51.60904",
|
||||
centre_longitude="-0.20629",
|
||||
sites=(
|
||||
Site(
|
||||
site_code="BA1",
|
||||
site_name="Barnet Test Site",
|
||||
site_type="Background",
|
||||
latitude="51.61000",
|
||||
longitude="-0.20500",
|
||||
updated=UPDATED,
|
||||
pollutants=(),
|
||||
status="no_species_data",
|
||||
number_of_pollutants=0,
|
||||
),
|
||||
),
|
||||
status="Low",
|
||||
)
|
||||
return LondonAirData(
|
||||
group_name="London",
|
||||
time_to_live=38,
|
||||
authorities={
|
||||
"Barnet": barnet,
|
||||
"City of London": city_of_london,
|
||||
"Merton": merton,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_london_air_client() -> Generator[AsyncMock]:
|
||||
"""Mock the London Air client."""
|
||||
with patch(
|
||||
"homeassistant.components.london_air.sensor.LondonAirClient"
|
||||
) as mock_client_cls:
|
||||
mock_client_cls.return_value.async_update = AsyncMock()
|
||||
yield mock_client_cls.return_value
|
||||
@@ -1,60 +0,0 @@
|
||||
{
|
||||
"HourlyAirQualityIndex": {
|
||||
"@GroupName": "London",
|
||||
"@TimeToLive": "38",
|
||||
"LocalAuthority": [
|
||||
{
|
||||
"@LocalAuthorityCode": "7",
|
||||
"@LocalAuthorityName": "City of London",
|
||||
"@LaCentreLatitude": "51.51333",
|
||||
"@LaCentreLongitude": "-0.088947",
|
||||
"@LaCentreLatitudeWGS84": "6712603.132989",
|
||||
"@LaCentreLongitudeWGS84": "-9901.534748"
|
||||
},
|
||||
{
|
||||
"@LocalAuthorityCode": "24",
|
||||
"@LocalAuthorityName": "Merton",
|
||||
"@LaCentreLatitude": "51.415672",
|
||||
"@LaCentreLongitude": "-0.191814",
|
||||
"@LaCentreLatitudeWGS84": "6695153.285882",
|
||||
"@LaCentreLongitudeWGS84": "-21352.636807",
|
||||
"Site": [
|
||||
{
|
||||
"@BulletinDate": "2017-08-03 03:00:00",
|
||||
"@SiteCode": "ME2",
|
||||
"@SiteName": "Merton - Merton Road",
|
||||
"@SiteType": "Roadside",
|
||||
"@Latitude": "51.4161384794862",
|
||||
"@Longitude": "-0.192230805042824",
|
||||
"@LatitudeWGS84": "6695236.54926",
|
||||
"@LongitudeWGS84": "-21399.0353321",
|
||||
"Species": {
|
||||
"@SpeciesCode": "PM10",
|
||||
"@SpeciesDescription": "PM10 Particulate",
|
||||
"@AirQualityIndex": "2",
|
||||
"@AirQualityBand": "Low",
|
||||
"@IndexSource": "Trigger"
|
||||
}
|
||||
},
|
||||
{
|
||||
"@BulletinDate": "2017-08-03 03:00:00",
|
||||
"@SiteCode": "ME9",
|
||||
"@SiteName": "Merton - Morden Civic Centre 2",
|
||||
"@SiteType": "Roadside",
|
||||
"@Latitude": "51.40162",
|
||||
"@Longitude": "-0.19589212",
|
||||
"@LatitudeWGS84": "6692543.79001",
|
||||
"@LongitudeWGS84": "-21810.7165116",
|
||||
"Species": {
|
||||
"@SpeciesCode": "NO2",
|
||||
"@SpeciesDescription": "Nitrogen Dioxide",
|
||||
"@AirQualityIndex": "1",
|
||||
"@AirQualityBand": "Low",
|
||||
"@IndexSource": "Measurement"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
"""The tests for the london_air platform."""
|
||||
|
||||
from http import HTTPStatus
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from pytest_homeassistant.common import async_load_fixture
|
||||
import requests_mock
|
||||
from pylondonair import (
|
||||
NO_SPECIES_DATA,
|
||||
LondonAirConnectionError,
|
||||
LondonAirData,
|
||||
LondonAirUpdateError,
|
||||
)
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.london_air.sensor import CONF_LOCATIONS, URL
|
||||
from homeassistant.components.london_air.sensor import CONF_LOCATIONS
|
||||
from homeassistant.const import ATTR_ENTITY_ID
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
@@ -13,14 +19,12 @@ VALID_CONFIG = {"sensor": {"platform": "london_air", CONF_LOCATIONS: ["Merton"]}
|
||||
|
||||
|
||||
async def test_valid_state(
|
||||
hass: HomeAssistant, requests_mock: requests_mock.Mocker
|
||||
hass: HomeAssistant,
|
||||
london_air_data: LondonAirData,
|
||||
mock_london_air_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test for operational london_air sensor with proper attributes."""
|
||||
requests_mock.get(
|
||||
URL,
|
||||
text=await async_load_fixture(hass, "london_air.json", "london_air"),
|
||||
status_code=HTTPStatus.OK,
|
||||
)
|
||||
mock_london_air_client.async_update.return_value = london_air_data
|
||||
assert await async_setup_component(hass, "sensor", VALID_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@@ -49,15 +53,107 @@ async def test_valid_state(
|
||||
assert pollutants[0]["summary"] == "PM10 is Low"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"error",
|
||||
[
|
||||
pytest.param(
|
||||
LondonAirConnectionError("Request to London Air API failed"),
|
||||
id="connection-error",
|
||||
),
|
||||
pytest.param(
|
||||
LondonAirUpdateError("Invalid JSON from London Air API"),
|
||||
id="update-error",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_api_failure(
|
||||
hass: HomeAssistant, requests_mock: requests_mock.Mocker
|
||||
hass: HomeAssistant,
|
||||
error: LondonAirConnectionError | LondonAirUpdateError,
|
||||
mock_london_air_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test for failure in the API."""
|
||||
requests_mock.get(URL, status_code=HTTPStatus.SERVICE_UNAVAILABLE)
|
||||
mock_london_air_client.async_update.side_effect = error
|
||||
assert await async_setup_component(hass, "sensor", VALID_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.merton")
|
||||
assert state is not None
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["updated"] is None
|
||||
assert state.attributes["sites"] == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("location", "entity_id"),
|
||||
[
|
||||
pytest.param(
|
||||
"City of London",
|
||||
"sensor.city_of_london",
|
||||
id="authority-without-sites",
|
||||
),
|
||||
pytest.param(
|
||||
"Barking and Dagenham",
|
||||
"sensor.barking_and_dagenham",
|
||||
id="authority-not-in-data",
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_authority_without_site_data(
|
||||
hass: HomeAssistant,
|
||||
location: str,
|
||||
entity_id: str,
|
||||
london_air_data: LondonAirData,
|
||||
mock_london_air_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test an authority with no usable site data."""
|
||||
config = {"sensor": {"platform": "london_air", CONF_LOCATIONS: [location]}}
|
||||
mock_london_air_client.async_update.return_value = london_air_data
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get(entity_id)
|
||||
assert state is not None
|
||||
assert state.state == "unknown"
|
||||
assert state.attributes["updated"] is None
|
||||
assert state.attributes["sites"] == 0
|
||||
|
||||
|
||||
async def test_site_without_pollutants(
|
||||
hass: HomeAssistant,
|
||||
london_air_data: LondonAirData,
|
||||
mock_london_air_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test a site that reports no pollutant data."""
|
||||
config = {"sensor": {"platform": "london_air", CONF_LOCATIONS: ["Barnet"]}}
|
||||
mock_london_air_client.async_update.return_value = london_air_data
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.barnet")
|
||||
assert state is not None
|
||||
assert state.state == "Low"
|
||||
site = state.attributes["data"][0]
|
||||
assert site["pollutants"] == [NO_SPECIES_DATA]
|
||||
assert site["pollutants_status"] == "no_species_data"
|
||||
assert site["number_of_pollutants"] == 0
|
||||
|
||||
|
||||
async def test_second_update_throttled(
|
||||
hass: HomeAssistant,
|
||||
london_air_data: LondonAirData,
|
||||
mock_london_air_client: AsyncMock,
|
||||
) -> None:
|
||||
"""Test that a second update within the scan interval does not refetch."""
|
||||
mock_london_air_client.async_update.return_value = london_air_data
|
||||
assert await async_setup_component(hass, "sensor", VALID_CONFIG)
|
||||
await hass.async_block_till_done()
|
||||
assert mock_london_air_client.async_update.await_count == 1
|
||||
|
||||
assert await async_setup_component(hass, "homeassistant", {})
|
||||
await hass.services.async_call(
|
||||
"homeassistant",
|
||||
"update_entity",
|
||||
{ATTR_ENTITY_ID: "sensor.merton"},
|
||||
blocking=True,
|
||||
)
|
||||
assert mock_london_air_client.async_update.await_count == 1
|
||||
|
||||
Reference in New Issue
Block a user