Skip to content

Commit

Permalink
fix: strings and error checking in config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronr committed Jul 29, 2024
1 parent 2e3ccc2 commit 332600a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 58 deletions.
10 changes: 4 additions & 6 deletions custom_components/doorking_1812ap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -56,15 +54,15 @@ 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)


async def async_reload_entry(
hass: HomeAssistant,
entry: IntegrationBlueprintConfigEntry,
entry: Doorking1812APConfigEntry,
) -> None:
"""Reload config entry."""
await async_unload_entry(hass, entry)
Expand Down
49 changes: 4 additions & 45 deletions custom_components/doorking_1812ap/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import socket
from typing import Any

import aiohttp
import async_timeout

from .const import LOGGER


Expand All @@ -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: {}"


Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions custom_components/doorking_1812ap/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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()
5 changes: 2 additions & 3 deletions custom_components/doorking_1812ap/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand All @@ -14,4 +14,3 @@
}
}
}

0 comments on commit 332600a

Please sign in to comment.