From 332600ab9504a40f5e2a247e201191850a100926 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Mon, 29 Jul 2024 13:54:21 -0700 Subject: [PATCH] fix: strings and error checking in config flow --- custom_components/doorking_1812ap/__init__.py | 10 ++-- custom_components/doorking_1812ap/api.py | 49 ++----------------- .../doorking_1812ap/config_flow.py | 6 +-- .../doorking_1812ap/translations/en.json | 5 +- 4 files changed, 12 insertions(+), 58 deletions(-) diff --git a/custom_components/doorking_1812ap/__init__.py b/custom_components/doorking_1812ap/__init__.py index df7a9dc..130b7d5 100644 --- a/custom_components/doorking_1812ap/__init__.py +++ b/custom_components/doorking_1812ap/__init__.py @@ -10,7 +10,6 @@ from typing import TYPE_CHECKING from homeassistant.const import CONF_IP_ADDRESS, Platform -from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.loader import async_get_loaded_integration from .api import Doorking1812APApiClient @@ -20,7 +19,7 @@ if TYPE_CHECKING: from homeassistant.core import HomeAssistant - from .data import IntegrationBlueprintConfigEntry + from .data import Doorking1812APConfigEntry PLATFORMS: list[Platform] = [ Platform.SWITCH, @@ -30,7 +29,7 @@ # https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry async def async_setup_entry( hass: HomeAssistant, - entry: IntegrationBlueprintConfigEntry, + entry: Doorking1812APConfigEntry, ) -> bool: """Set up this integration using UI.""" coordinator = Doorking1812APDataUpdateCoordinator( @@ -39,7 +38,6 @@ async def async_setup_entry( entry.runtime_data = Doorking1812APData( client=Doorking1812APApiClient( ip_address=entry.data[CONF_IP_ADDRESS], - session=async_get_clientsession(hass), ), integration=async_get_loaded_integration(hass, entry.domain), coordinator=coordinator, @@ -56,7 +54,7 @@ async def async_setup_entry( async def async_unload_entry( hass: HomeAssistant, - entry: IntegrationBlueprintConfigEntry, + entry: Doorking1812APConfigEntry, ) -> bool: """Handle removal of an entry.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) @@ -64,7 +62,7 @@ async def async_unload_entry( async def async_reload_entry( hass: HomeAssistant, - entry: IntegrationBlueprintConfigEntry, + entry: Doorking1812APConfigEntry, ) -> None: """Reload config entry.""" await async_unload_entry(hass, entry) diff --git a/custom_components/doorking_1812ap/api.py b/custom_components/doorking_1812ap/api.py index 32bd61c..1803ea8 100644 --- a/custom_components/doorking_1812ap/api.py +++ b/custom_components/doorking_1812ap/api.py @@ -6,9 +6,6 @@ import socket from typing import Any -import aiohttp -import async_timeout - from .const import LOGGER @@ -22,11 +19,6 @@ class Doorking1812APApiClientCommunicationError( """Exception to indicate a communication error.""" -def _verify_response_or_raise(response: aiohttp.ClientResponse) -> None: - """Verify that the response is valid.""" - response.raise_for_status() - - UNEXPECTED_DATA_ERROR = "Unexpected data received: {}" @@ -44,11 +36,9 @@ class Doorking1812APApiClient: def __init__( self, ip_address: str, - session: aiohttp.ClientSession, ) -> None: """Sample API Client.""" self._ip_address = ip_address - self._session = session async def connect_to_server( self, @@ -109,6 +99,8 @@ async def async_get_data(self) -> Any: LOGGER.debug("gate is unknown") _raise_unexpected_data_error(data) + except Doorking1812APApiClientCommunicationError: + raise except Exception as exception: # pylint: disable=broad-except msg = f"Something really wrong happened! - {exception}" raise Doorking1812APApiClientError( @@ -131,41 +123,8 @@ async def async_open_gate(self, *, close_gate: bool = False) -> Any: writer.close() await writer.wait_closed() - except Exception as exception: # pylint: disable=broad-except - msg = f"Something really wrong happened! - {exception}" - raise Doorking1812APApiClientError( - msg, - ) from exception - - async def _api_wrapper( - self, - method: str, - url: str, - data: dict | None = None, - headers: dict | None = None, - ) -> Any: - """Get information from the API.""" - try: - async with async_timeout.timeout(10): - response = await self._session.request( - method=method, - url=url, - headers=headers, - json=data, - ) - _verify_response_or_raise(response) - return await response.json() - - except TimeoutError as exception: - msg = f"Timeout error fetching information - {exception}" - raise Doorking1812APApiClientCommunicationError( - msg, - ) from exception - except (aiohttp.ClientError, socket.gaierror) as exception: - msg = f"Error fetching information - {exception}" - raise Doorking1812APApiClientCommunicationError( - msg, - ) from exception + except Doorking1812APApiClientCommunicationError: + raise except Exception as exception: # pylint: disable=broad-except msg = f"Something really wrong happened! - {exception}" raise Doorking1812APApiClientError( diff --git a/custom_components/doorking_1812ap/config_flow.py b/custom_components/doorking_1812ap/config_flow.py index 53555d0..63da20c 100644 --- a/custom_components/doorking_1812ap/config_flow.py +++ b/custom_components/doorking_1812ap/config_flow.py @@ -6,7 +6,6 @@ from homeassistant import config_entries, data_entry_flow from homeassistant.const import CONF_IP_ADDRESS from homeassistant.helpers import selector -from homeassistant.helpers.aiohttp_client import async_create_clientsession from .api import ( Doorking1812APApiClient, @@ -16,7 +15,7 @@ from .const import DOMAIN, LOGGER -class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): +class Doorking1812APFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Config flow for Blueprint.""" VERSION = 1 @@ -62,10 +61,9 @@ async def async_step_user( ) async def _test_connection(self, ip_address: str) -> None: - """Validate credentials.""" + """Test the connection.""" client = Doorking1812APApiClient( ip_address=ip_address, - session=async_create_clientsession(self.hass), ) await client.async_get_data() diff --git a/custom_components/doorking_1812ap/translations/en.json b/custom_components/doorking_1812ap/translations/en.json index 30d178d..e557c4d 100644 --- a/custom_components/doorking_1812ap/translations/en.json +++ b/custom_components/doorking_1812ap/translations/en.json @@ -2,9 +2,9 @@ "config": { "step": { "user": { - "description": "If you need help with the configuration have a look here: https://github.com/cameronr/doorking-ha", + "description": "Enter the IP address of the Doorking 1812AP", "data": { - "ip": "" + "ip_address": "IP address" } } }, @@ -14,4 +14,3 @@ } } } -