mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
parent
99f227229e
commit
727074a1a6
@ -3,11 +3,10 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from aiohttp import ClientError
|
from aiohttp import ClientError
|
||||||
from aussiebb.asyncio import AussieBB
|
from aussiebb.asyncio import AussieBB
|
||||||
from aussiebb.const import FETCH_TYPES, NBN_TYPES, PHONE_TYPES
|
from aussiebb.const import FETCH_TYPES
|
||||||
from aussiebb.exceptions import AuthenticationException, UnrecognisedServiceType
|
from aussiebb.exceptions import AuthenticationException, UnrecognisedServiceType
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -23,19 +22,6 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
PLATFORMS = [Platform.SENSOR]
|
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:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up Aussie Broadband from a config entry."""
|
"""Set up Aussie Broadband from a config entry."""
|
||||||
# Login to the Aussie Broadband API and retrieve the current service list
|
# Login to the Aussie Broadband API and retrieve the current service list
|
||||||
@ -44,9 +30,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
entry.data[CONF_PASSWORD],
|
entry.data[CONF_PASSWORD],
|
||||||
async_get_clientsession(hass),
|
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:
|
try:
|
||||||
await client.login()
|
await client.login()
|
||||||
services = await client.get_services(drop_types=FETCH_TYPES)
|
services = await client.get_services(drop_types=FETCH_TYPES)
|
||||||
|
@ -3,11 +3,8 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
from aiohttp import ClientConnectionError
|
from aiohttp import ClientConnectionError
|
||||||
from aussiebb.exceptions import AuthenticationException, UnrecognisedServiceType
|
from aussiebb.exceptions import AuthenticationException, UnrecognisedServiceType
|
||||||
import pydantic
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
from homeassistant.components.aussie_broadband import validate_service_type
|
|
||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
@ -22,19 +19,6 @@ async def test_unload(hass: HomeAssistant) -> None:
|
|||||||
assert entry.state is ConfigEntryState.NOT_LOADED
|
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:
|
async def test_auth_failure(hass: HomeAssistant) -> None:
|
||||||
"""Test init with an authentication failure."""
|
"""Test init with an authentication failure."""
|
||||||
with patch(
|
with patch(
|
||||||
@ -55,9 +39,3 @@ async def test_service_failure(hass: HomeAssistant) -> None:
|
|||||||
"""Test init with a invalid service."""
|
"""Test init with a invalid service."""
|
||||||
entry = await setup_platform(hass, usage_effect=UnrecognisedServiceType())
|
entry = await setup_platform(hass, usage_effect=UnrecognisedServiceType())
|
||||||
assert entry.state is ConfigEntryState.SETUP_RETRY
|
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"
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user