Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch service validation in Aussie Broadband #99077

Merged
merged 12 commits into from
Aug 31, 2023
19 changes: 18 additions & 1 deletion homeassistant/components/aussie_broadband/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

from datetime import timedelta
import logging
from typing import Any

from aiohttp import ClientError
from aussiebb.asyncio import AussieBB
from aussiebb.const import FETCH_TYPES
from aussiebb.const import FETCH_TYPES, NBN_TYPES, PHONE_TYPES
from aussiebb.exceptions import AuthenticationException, UnrecognisedServiceType

from homeassistant.config_entries import ConfigEntry
Expand All @@ -22,6 +23,19 @@
PLATFORMS = [Platform.SENSOR]


# Backport for the pyaussiebb=0.0.15 validate_service_type method
def validate_service_type(service: dict[str, Any]) -> None:
"""Check the service types against known types."""

if "type" not in service:
raise ValueError("Field 'type' not found in service data")
if service["type"] not in NBN_TYPES + PHONE_TYPES + ["Hardware"]:
raise UnrecognisedServiceType(
f"Service type {service['type']=} {service['name']=} - not recognised - ",
"please report this at https://github.com/yaleman/aussiebb/issues/new",
)


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Aussie Broadband from a config entry."""
# Login to the Aussie Broadband API and retrieve the current service list
Expand All @@ -30,6 +44,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.data[CONF_PASSWORD],
async_get_clientsession(hass),
)
# Overwrite the pyaussiebb=0.0.15 validate_service_type method with backport
# Required until pydantic 2.x is supported
client.validate_service_type = validate_service_type
try:
await client.login()
services = await client.get_services(drop_types=FETCH_TYPES)
Expand Down
22 changes: 22 additions & 0 deletions tests/components/aussie_broadband/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

from aiohttp import ClientConnectionError
from aussiebb.exceptions import AuthenticationException, UnrecognisedServiceType
import pydantic
import pytest

from homeassistant import data_entry_flow
from homeassistant.components.aussie_broadband import validate_service_type
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant

Expand All @@ -19,6 +22,19 @@ async def test_unload(hass: HomeAssistant) -> None:
assert entry.state is ConfigEntryState.NOT_LOADED


async def test_validate_service_type() -> None:
"""Testing the validation function."""
test_service = {"type": "Hardware", "name": "test service"}
validate_service_type(test_service)

with pytest.raises(ValueError):
test_service = {"name": "test service"}
validate_service_type(test_service)
with pytest.raises(UnrecognisedServiceType):
test_service = {"type": "FunkyBob", "name": "test service"}
validate_service_type(test_service)


async def test_auth_failure(hass: HomeAssistant) -> None:
"""Test init with an authentication failure."""
with patch(
Expand All @@ -39,3 +55,9 @@ async def test_service_failure(hass: HomeAssistant) -> None:
"""Test init with a invalid service."""
entry = await setup_platform(hass, usage_effect=UnrecognisedServiceType())
assert entry.state is ConfigEntryState.SETUP_RETRY


async def test_not_pydantic2() -> None:
"""Test that Home Assistant still does not support Pydantic 2."""
"""For PR#99077 and validate_service_type backport"""
assert pydantic.__version__ < "2"