mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Set Home Assistant User-Agent for Fuelprices.dk (#193774)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
co-authored by
Paulus Schoutsen
parent
6c6889c48d
commit
30bd9e490a
@@ -15,6 +15,7 @@ from homeassistant.config_entries import (
|
||||
)
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.aiohttp_client import SERVER_SOFTWARE
|
||||
|
||||
from . import FuelpricesDkConfigEntry
|
||||
from .const import (
|
||||
@@ -71,7 +72,7 @@ class FuelpricesDkConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
self, api_key: str
|
||||
) -> tuple[Braendstofpriser | None, list[dict[str, Any]], str | None]:
|
||||
"""Validate the API key and fetch available companies."""
|
||||
api = Braendstofpriser(api_key)
|
||||
api = Braendstofpriser(api_key, user_agent=SERVER_SOFTWARE)
|
||||
try:
|
||||
companies = await api.list_companies()
|
||||
except ClientResponseError as exc:
|
||||
@@ -219,7 +220,7 @@ class FuelpricesDkConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
errors: dict[str, str] = {}
|
||||
|
||||
if user_input is not None:
|
||||
api = Braendstofpriser(user_input[CONF_API_KEY])
|
||||
api = Braendstofpriser(user_input[CONF_API_KEY], user_agent=SERVER_SOFTWARE)
|
||||
try:
|
||||
await api.list_companies()
|
||||
except ClientResponseError as exc:
|
||||
@@ -300,7 +301,7 @@ class FuelpricesDkStationSubentryFlow(ConfigSubentryFlow):
|
||||
"""Initialize API client and fetch companies."""
|
||||
entry = self._get_entry()
|
||||
api_key = entry.data[CONF_API_KEY]
|
||||
self.api = Braendstofpriser(api_key)
|
||||
self.api = Braendstofpriser(api_key, user_agent=SERVER_SOFTWARE)
|
||||
try:
|
||||
self.companies = await self.api.list_companies()
|
||||
except ClientResponseError as exc:
|
||||
|
||||
@@ -11,6 +11,7 @@ from pybraendstofpriser import Braendstofpriser
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers.aiohttp_client import SERVER_SOFTWARE
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import CONF_STATION, DOMAIN, SUBENTRY_TYPE_STATION
|
||||
@@ -48,7 +49,9 @@ class FuelPricesDKCoordinator(DataUpdateCoordinator[dict[int, FuelPricesDKStatio
|
||||
config_entry=config_entry,
|
||||
)
|
||||
|
||||
self._api = Braendstofpriser(config_entry.data[CONF_API_KEY])
|
||||
self._api = Braendstofpriser(
|
||||
config_entry.data[CONF_API_KEY], user_agent=SERVER_SOFTWARE
|
||||
)
|
||||
self._station_ids = [
|
||||
subentry.data[CONF_STATION]["id"]
|
||||
for subentry in config_entry.get_subentries_of_type(SUBENTRY_TYPE_STATION)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
"""Test the Fuelprices.dk config flow."""
|
||||
|
||||
from collections.abc import Callable
|
||||
from unittest.mock import AsyncMock, Mock
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock
|
||||
|
||||
from aiohttp import ClientResponseError
|
||||
from pybraendstofpriser import Flist
|
||||
@@ -13,6 +13,7 @@ from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
from homeassistant.helpers.aiohttp_client import SERVER_SOFTWARE
|
||||
|
||||
from . import setup_integration
|
||||
from .conftest import TEST_API_KEY, TEST_COMPANY, TEST_STATION
|
||||
@@ -34,6 +35,7 @@ def _client_error(status: int) -> ClientResponseError:
|
||||
async def test_full_user_flow(
|
||||
hass: HomeAssistant,
|
||||
mock_braendstofpriser: AsyncMock,
|
||||
mock_braendstofpriser_class: MagicMock,
|
||||
mock_setup_entry: AsyncMock,
|
||||
) -> None:
|
||||
"""Test a full successful config flow."""
|
||||
@@ -70,6 +72,9 @@ async def test_full_user_flow(
|
||||
assert subentry["unique_id"] == f"{TEST_COMPANY}_{TEST_STATION['id']}"
|
||||
assert subentry["data"] == {"company": TEST_COMPANY, "station": TEST_STATION}
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
mock_braendstofpriser_class.assert_called_once_with(
|
||||
TEST_API_KEY, user_agent=SERVER_SOFTWARE
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
@@ -303,6 +308,7 @@ async def test_reauth_success(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
mock_braendstofpriser: AsyncMock,
|
||||
mock_braendstofpriser_class: MagicMock,
|
||||
) -> None:
|
||||
"""Test reauthentication updates the API key."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
@@ -318,6 +324,9 @@ async def test_reauth_success(
|
||||
assert result["type"] is FlowResultType.ABORT
|
||||
assert result["reason"] == "reauth_successful"
|
||||
assert mock_config_entry.data[CONF_API_KEY] == "new-api-key"
|
||||
mock_braendstofpriser_class.assert_called_once_with(
|
||||
"new-api-key", user_agent=SERVER_SOFTWARE
|
||||
)
|
||||
|
||||
|
||||
async def test_reauth_rejects_api_key_used_by_another_entry(
|
||||
@@ -379,6 +388,7 @@ async def test_reauth_recovers_from_api_errors(
|
||||
async def test_subentry_flow_create(
|
||||
hass: HomeAssistant,
|
||||
mock_braendstofpriser: AsyncMock,
|
||||
mock_braendstofpriser_class: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test creating a station subentry."""
|
||||
@@ -409,6 +419,9 @@ async def test_subentry_flow_create(
|
||||
assert result["type"] is FlowResultType.CREATE_ENTRY
|
||||
assert result["title"] == f"{TEST_COMPANY} - {new_station['name']}"
|
||||
assert result["unique_id"] == f"{TEST_COMPANY}_{new_station['id']}"
|
||||
mock_braendstofpriser_class.assert_called_once_with(
|
||||
TEST_API_KEY, user_agent=SERVER_SOFTWARE
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
|
||||
@@ -20,6 +20,7 @@ from homeassistant.config_entries import (
|
||||
from homeassistant.const import CONF_API_KEY
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
from homeassistant.helpers.aiohttp_client import SERVER_SOFTWARE
|
||||
|
||||
from . import client_error, setup_integration
|
||||
from .conftest import TEST_API_KEY, TEST_COMPANY, TEST_PRICES, TEST_STATION
|
||||
@@ -28,6 +29,7 @@ from .conftest import TEST_API_KEY, TEST_COMPANY, TEST_PRICES, TEST_STATION
|
||||
async def test_setup_and_unload_entry(
|
||||
hass: HomeAssistant,
|
||||
mock_braendstofpriser: AsyncMock,
|
||||
mock_braendstofpriser_class: MagicMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
) -> None:
|
||||
@@ -35,6 +37,9 @@ async def test_setup_and_unload_entry(
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
mock_braendstofpriser_class.assert_called_once_with(
|
||||
TEST_API_KEY, user_agent=SERVER_SOFTWARE
|
||||
)
|
||||
|
||||
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
@@ -207,10 +212,10 @@ async def test_api_keys_update_independently(
|
||||
) -> None:
|
||||
"""Test separate API keys have their own station selections and updates."""
|
||||
data = await async_load_json_array_fixture(hass, "bulk_prices.json", DOMAIN)
|
||||
mock_braendstofpriser_class.side_effect = {
|
||||
TEST_API_KEY: mock_braendstofpriser,
|
||||
"other-api-key": mock_other_braendstofpriser,
|
||||
}.__getitem__
|
||||
mock_braendstofpriser_class.side_effect = [
|
||||
mock_braendstofpriser,
|
||||
mock_other_braendstofpriser,
|
||||
]
|
||||
mock_braendstofpriser.get_prices_bulk.return_value = data[:1]
|
||||
mock_other_braendstofpriser.get_prices_bulk.return_value = data[1:]
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
@@ -229,8 +234,8 @@ async def test_api_keys_update_independently(
|
||||
await setup_integration(hass, other_entry)
|
||||
|
||||
assert mock_braendstofpriser_class.call_args_list == [
|
||||
call(TEST_API_KEY),
|
||||
call("other-api-key"),
|
||||
call(TEST_API_KEY, user_agent=SERVER_SOFTWARE),
|
||||
call("other-api-key", user_agent=SERVER_SOFTWARE),
|
||||
]
|
||||
mock_braendstofpriser.get_prices_bulk.assert_awaited_once_with([1234])
|
||||
mock_other_braendstofpriser.get_prices_bulk.assert_awaited_once_with([4321])
|
||||
|
||||
Reference in New Issue
Block a user