Rename get_deconz_session to get_deconz_api (#112826)

Move and rename get_deconz_session to get_deconz_api
This commit is contained in:
Robert Svensson 2024-03-09 18:32:31 +01:00 committed by GitHub
parent 9ba142f4dd
commit 03e4a20cdf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 57 additions and 51 deletions

View File

@ -19,7 +19,8 @@ from .config_flow import get_master_gateway
from .const import CONF_GROUP_ID_BASE, CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS
from .deconz_event import async_setup_events, async_unload_events
from .errors import AuthenticationRequired, CannotConnect
from .gateway import DeconzGateway, get_deconz_session
from .gateway import DeconzGateway
from .hub import get_deconz_api
from .services import async_setup_services, async_unload_services
@ -37,7 +38,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
await async_update_master_gateway(hass, config_entry)
try:
api = await get_deconz_session(hass, config_entry.data)
api = await get_deconz_api(hass, config_entry.data)
except CannotConnect as err:
raise ConfigEntryNotReady from err
except AuthenticationRequired as err:

View File

@ -2,25 +2,19 @@
from __future__ import annotations
import asyncio
from collections.abc import Callable
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, cast
from typing import TYPE_CHECKING, cast
from pydeconz import DeconzSession, errors
from pydeconz import DeconzSession
from pydeconz.interfaces import sensors
from pydeconz.interfaces.api_handlers import APIHandler, GroupedAPIHandler
from pydeconz.interfaces.groups import GroupHandler
from pydeconz.models.event import EventType
from homeassistant.config_entries import SOURCE_HASSIO, ConfigEntry
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import Event, HomeAssistant, callback
from homeassistant.helpers import (
aiohttp_client,
device_registry as dr,
entity_registry as er,
)
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_send
@ -34,10 +28,8 @@ from .const import (
DEFAULT_ALLOW_NEW_DEVICES,
DOMAIN as DECONZ_DOMAIN,
HASSIO_CONFIGURATION_URL,
LOGGER,
PLATFORMS,
)
from .errors import AuthenticationRequired, CannotConnect
if TYPE_CHECKING:
from .deconz_event import (
@ -339,30 +331,3 @@ def get_gateway_from_config_entry(
) -> DeconzGateway:
"""Return gateway with a matching config entry ID."""
return cast(DeconzGateway, hass.data[DECONZ_DOMAIN][config_entry.entry_id])
async def get_deconz_session(
hass: HomeAssistant,
config: MappingProxyType[str, Any],
) -> DeconzSession:
"""Create a gateway object and verify configuration."""
session = aiohttp_client.async_get_clientsession(hass)
deconz_session = DeconzSession(
session,
config[CONF_HOST],
config[CONF_PORT],
config[CONF_API_KEY],
)
try:
async with asyncio.timeout(10):
await deconz_session.refresh_state()
return deconz_session
except errors.Unauthorized as err:
LOGGER.warning("Invalid key for deCONZ at %s", config[CONF_HOST])
raise AuthenticationRequired from err
except (TimeoutError, errors.RequestError, errors.ResponseError) as err:
LOGGER.error("Error connecting to deCONZ gateway at %s", config[CONF_HOST])
raise CannotConnect from err

View File

@ -0,0 +1,3 @@
"""Internal functionality not part of HA infrastructure."""
from .api import get_deconz_api # noqa: F401

View File

@ -0,0 +1,39 @@
"""deCONZ API representation."""
from __future__ import annotations
import asyncio
from types import MappingProxyType
from typing import Any
from pydeconz import DeconzSession, errors
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.helpers import aiohttp_client
from ..const import LOGGER
from ..errors import AuthenticationRequired, CannotConnect
async def get_deconz_api(
hass: HomeAssistant, config: MappingProxyType[str, Any]
) -> DeconzSession:
"""Create a gateway object and verify configuration."""
session = aiohttp_client.async_get_clientsession(hass)
api = DeconzSession(
session, config[CONF_HOST], config[CONF_PORT], config[CONF_API_KEY]
)
try:
async with asyncio.timeout(10):
await api.refresh_state()
return api
except errors.Unauthorized as err:
LOGGER.warning("Invalid key for deCONZ at %s", config[CONF_HOST])
raise AuthenticationRequired from err
except (TimeoutError, errors.RequestError, errors.ResponseError) as err:
LOGGER.error("Error connecting to deCONZ gateway at %s", config[CONF_HOST])
raise CannotConnect from err

View File

@ -18,10 +18,8 @@ from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
from homeassistant.components.deconz.config_flow import DECONZ_MANUFACTURERURL
from homeassistant.components.deconz.const import DOMAIN as DECONZ_DOMAIN
from homeassistant.components.deconz.errors import AuthenticationRequired, CannotConnect
from homeassistant.components.deconz.gateway import (
get_deconz_session,
get_gateway_from_config_entry,
)
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
from homeassistant.components.deconz.hub import get_deconz_api
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
@ -288,10 +286,10 @@ async def test_reset_after_successful_setup(
assert result is True
async def test_get_deconz_session(hass: HomeAssistant) -> None:
async def test_get_deconz_api(hass: HomeAssistant) -> None:
"""Successful call."""
with patch("pydeconz.DeconzSession.refresh_state", return_value=True):
assert await get_deconz_session(hass, ENTRY_CONFIG)
assert await get_deconz_api(hass, ENTRY_CONFIG)
@pytest.mark.parametrize(
@ -303,7 +301,7 @@ async def test_get_deconz_session(hass: HomeAssistant) -> None:
(pydeconz.Unauthorized, AuthenticationRequired),
],
)
async def test_get_deconz_session_fails(
async def test_get_deconz_api_fails(
hass: HomeAssistant, side_effect, raised_exception
) -> None:
"""Failed call."""
@ -311,4 +309,4 @@ async def test_get_deconz_session_fails(
"pydeconz.DeconzSession.refresh_state",
side_effect=side_effect,
), pytest.raises(raised_exception):
assert await get_deconz_session(hass, ENTRY_CONFIG)
assert await get_deconz_api(hass, ENTRY_CONFIG)

View File

@ -59,7 +59,7 @@ async def test_setup_entry_successful(
async def test_setup_entry_fails_config_entry_not_ready(hass: HomeAssistant) -> None:
"""Failed authentication trigger a reauthentication flow."""
with patch(
"homeassistant.components.deconz.get_deconz_session",
"homeassistant.components.deconz.get_deconz_api",
side_effect=CannotConnect,
):
await setup_deconz_integration(hass)
@ -70,7 +70,7 @@ async def test_setup_entry_fails_config_entry_not_ready(hass: HomeAssistant) ->
async def test_setup_entry_fails_trigger_reauth_flow(hass: HomeAssistant) -> None:
"""Failed authentication trigger a reauthentication flow."""
with patch(
"homeassistant.components.deconz.get_deconz_session",
"homeassistant.components.deconz.get_deconz_api",
side_effect=AuthenticationRequired,
), patch.object(hass.config_entries.flow, "async_init") as mock_flow_init:
await setup_deconz_integration(hass)