Skip to content

Commit

Permalink
fix: config flow and domain constant (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Djelibeybi authored Aug 18, 2024
1 parent 612a236 commit f71640f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 32 deletions.
5 changes: 3 additions & 2 deletions custom_components/exoy_one/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def __init__(
super().__init__(coordinator)
self.entity_description = entity_description
self._attr_name = entity_description.name
self.entity_id = f"binary_sensor.{self.coordinator.exoyone.state.mdnsName}_{entity_description.key}"
self._attr_unique_id = f"{self.coordinator.exoyone.state.mdnsName}_binary_sensor_{entity_description.key}"
mdns_name = self.coordinator.exoyone.state.mdnsName
self.entity_id = f"binary_sensor.{mdns_name}_{entity_description.key}"
self._attr_unique_id = f"{mdns_name}_binary_sensor_{entity_description.key}"

@property
def is_on(self) -> bool:
Expand Down
44 changes: 15 additions & 29 deletions custom_components/exoy_one/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from typing import TYPE_CHECKING

import voluptuous as vol
from exoyone import ExoyOne
from exoyone.models import ExoyOneException, ExoyOneTimeoutError
from exoyone import ExoyOne, ExoyOneTimeoutError
from homeassistant import config_entries, data_entry_flow
from homeassistant.const import CONF_HOST, CONF_IP_ADDRESS
from homeassistant.helpers import selector
Expand Down Expand Up @@ -52,11 +51,8 @@ async def _async_handle_discovery(
(progress.get("context", {}).get(CONF_HOST) == hostname),
):
return self.async_abort(reason="already_in_progress")
if not (
device := await self._async_try_connect(
hostname, ip_address, raise_on_progress=True
)
):

if not (device := await _async_try_connect(ip_address)):
LOGGER.debug("Failed to connect to %s (%s)", hostname, ip_address)
return self.async_abort(reason="cannot_connect")

Expand All @@ -73,28 +69,6 @@ async def _async_handle_discovery(
}
)

async def _async_try_connect(
self,
hostname: str,
ip_address: str,
raise_on_progress: bool = True, # noqa: ARG002, FBT001, FBT002
) -> ExoyOne | None:
"""Try to connect to the ExoyONE."""
try:
exoyone = ExoyOne(host=ip_address)
await exoyone.async_get_data()
except socket.gaierror:
return None
except ExoyOneTimeoutError as exception:
if raise_on_progress:
raise data_entry_flow.AbortFlow(reason="cannot_connect") from exception
return None
except ExoyOneException as exception:
if raise_on_progress:
raise data_entry_flow.AbortFlow(reason="cannot_connect") from exception
return None
return exoyone

async def async_step_discovery_confirm(
self, user_input: dict[str, str] | None = None
) -> ConfigFlowResult:
Expand Down Expand Up @@ -139,3 +113,15 @@ async def async_step_user(
),
errors=_errors,
)


async def _async_try_connect(ip_address: str) -> ExoyOne | None:
"""Try to connect to the ExoyONE."""
try:
exoyone = ExoyOne(host=ip_address)
await exoyone.async_get_data()
except socket.gaierror:
return None
except ExoyOneTimeoutError as exception:
raise data_entry_flow.AbortFlow(reason="cannot_connect") from exception
return exoyone
2 changes: 1 addition & 1 deletion custom_components/exoy_one/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

LOGGER: Logger = getLogger(__package__)

DOMAIN = "exoyone"
DOMAIN = "exoy_one"

ATTR_STATE = "state"

0 comments on commit f71640f

Please sign in to comment.