Files
core/tests/components/metoffice/test_init.py
avee87 4f24d63de1 Update metoffice to use DataHub API (#131425)
* Update metoffice to use DataHub API

* Reauth test

* Updated to datapoint 0.11.0

* Less hacky check for day/night in twice-daily forecasts

* Updated to datapoint 0.12.1, added daily forecast

* addressed review comments

* one more nit

* validate credewntials in reauth flow

* Addressed review comments

* Attempt to improve coverage

* Addressed comments

* Reverted unnecessary reordering

* Update homeassistant/components/metoffice/sensor.py

* Update tests/components/metoffice/test_sensor.py

* Update homeassistant/components/metoffice/sensor.py

---------

Co-authored-by: Franck Nijhof <git@frenck.dev>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2025-05-21 21:56:32 +02:00

66 lines
2.0 KiB
Python

"""Tests for metoffice init."""
import datetime
import json
import pytest
import requests_mock
from homeassistant.components.metoffice.const import DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.util import utcnow
from .const import METOFFICE_CONFIG_WAVERTREE
from tests.common import MockConfigEntry, async_fire_time_changed, load_fixture
@pytest.mark.freeze_time(datetime.datetime(2024, 11, 23, 12, tzinfo=datetime.UTC))
async def test_reauth_on_auth_error(
hass: HomeAssistant,
requests_mock: requests_mock.Mocker,
device_registry: dr.DeviceRegistry,
) -> None:
"""Test handling authentication errors and reauth flow."""
mock_json = json.loads(load_fixture("metoffice.json", "metoffice"))
wavertree_daily = json.dumps(mock_json["wavertree_daily"])
wavertree_hourly = json.dumps(mock_json["wavertree_hourly"])
requests_mock.get(
"https://data.hub.api.metoffice.gov.uk/sitespecific/v0/point/daily",
text=wavertree_daily,
)
requests_mock.get(
"https://data.hub.api.metoffice.gov.uk/sitespecific/v0/point/hourly",
text=wavertree_hourly,
)
entry = MockConfigEntry(
domain=DOMAIN,
data=METOFFICE_CONFIG_WAVERTREE,
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(device_registry.devices) == 1
requests_mock.get(
"https://data.hub.api.metoffice.gov.uk/sitespecific/v0/point/daily",
text="",
status_code=401,
)
requests_mock.get(
"https://data.hub.api.metoffice.gov.uk/sitespecific/v0/point/hourly",
text="",
status_code=401,
)
future_time = utcnow() + datetime.timedelta(minutes=40)
async_fire_time_changed(hass, future_time)
await hass.async_block_till_done(wait_background_tasks=True)
flows = hass.config_entries.flow.async_progress()
assert len(flows) == 1
assert flows[0]["step_id"] == "reauth_confirm"