Skip to content

Commit

Permalink
Fixes broken manual setup (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohNan authored Oct 26, 2023
1 parent 6aeaf91 commit 48ab6f3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
34 changes: 25 additions & 9 deletions custom_components/flichub/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Adds config flow for Flic Hub."""
from homeassistant.data_entry_flow import FlowResult
from typing import Any

import async_timeout
Expand Down Expand Up @@ -30,34 +31,39 @@ def __init__(self):
self._mac_address = None
self._errors = {}

async def async_step_dhcp(self, discovery_info):
async def async_step_dhcp(self, discovery_info) -> FlowResult:
"""Handle dhcp discovery."""
self._ip_address = discovery_info.ip
self._mac_address = discovery_info.macaddress

self._async_abort_entries_match({CONF_IP_ADDRESS: self._ip_address})
await self.async_set_unique_id(format_mac(self._mac_address))
self._abort_if_unique_id_configured()
self._abort_if_unique_id_configured(
updates={
CONF_IP_ADDRESS: self._ip_address
}
)

self.context["title_placeholders"] = {CONF_IP_ADDRESS: self._ip_address}
return await self.async_step_user()

async def async_step_user(self, user_input: dict[str, Any] | None = None):
async def async_step_user(self, user_input: dict[str, Any] | None = None) -> FlowResult:
"""Handle a flow initialized by the user."""
self._errors = {}

if user_input is not None:
valid = await self._test_credentials(
valid, mac = await self._test_credentials(
user_input[CONF_IP_ADDRESS],
user_input[CONF_PORT]
)
if valid:
self._mac_address = mac
return await self._create_entry(user_input)
self._errors["base"] = "cannot_connect"

return await self._show_config_form(user_input)

async def _create_entry(self, user_input):
async def _create_entry(self, user_input) -> FlowResult:
existing_entry = await self.async_set_unique_id(
format_mac(self._mac_address)
)
Expand All @@ -77,7 +83,7 @@ async def _create_entry(self, user_input):
return self.async_abort(reason="reauth_successful")
return self.async_create_entry(title=user_input[CONF_NAME], data=user_input)

async def _show_config_form(self, user_input: dict[str, Any] | None = None):
async def _show_config_form(self, user_input: dict[str, Any] | None = None) -> FlowResult:
"""Show the configuration form to edit location data."""
if user_input is None:
return self.async_show_form(
Expand All @@ -104,7 +110,8 @@ async def _show_config_form(self, user_input: dict[str, Any] | None = None):
errors=self._errors,
)

async def _test_credentials(self, ip, port):
@staticmethod
async def _test_credentials(ip, port) -> tuple[bool, str | None]:
"""Return true if credentials is valid."""
client = FlicHubTcpClient(ip, port, asyncio.get_event_loop())
try:
Expand All @@ -124,13 +131,22 @@ async def client_disconnected():
with async_timeout.timeout(CLIENT_READY_TIMEOUT):
await client_ready.wait()

hub_info = await client.get_hubinfo()
if hub_info.has_wifi() and hub_info.wifi.ip == ip:
client.disconnect()
return True, hub_info.wifi.mac

if hub_info.has_ethernet() and hub_info.ethernet.ip == ip:
client.disconnect()
return True, hub_info.ethernet.mac

client.disconnect()
return True
return False, None
except Exception as e: # pylint: disable=broad-except
_LOGGER.error("Error connecting", exc_info=e)
client.disconnect()
pass
return False
return False, None

@staticmethod
@callback
Expand Down
10 changes: 6 additions & 4 deletions custom_components/flichub/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,33 @@ def __init__(self, coordinator, config_entry, flic_hub: FlicHubInfo):
super().__init__(coordinator)
self.coordinator = coordinator
self._flic_hub = flic_hub
self._ip_address = config_entry.data[CONF_IP_ADDRESS]
self.config_entry = config_entry

@property
def mac_address(self):
"""Return a unique ID to use for this entity."""
if self.flic_hub.has_ethernet():
if self.flic_hub.has_ethernet() and self._ip_address == self.flic_hub.ethernet.ip:
return format_mac(self.flic_hub.ethernet.mac)
if self.flic_hub.has_wifi():
if self.flic_hub.has_wifi() and self._ip_address == self.flic_hub.wifi.ip:
return format_mac(self.flic_hub.wifi.mac)

@property
def device_info(self):
identifiers = set()
connections = set()

if self.flic_hub.has_ethernet():
if self.flic_hub.has_ethernet() and self._ip_address == self.flic_hub.ethernet.ip:
identifiers.add((DOMAIN, format_mac(self.flic_hub.ethernet.mac)))
connections.add((DOMAIN, format_mac(self.flic_hub.ethernet.mac)))
if self.flic_hub.has_wifi():
if self.flic_hub.has_wifi() and self._ip_address == self.flic_hub.wifi.ip:
identifiers.add((DOMAIN, format_mac(self.flic_hub.wifi.mac)))
connections.add((DOMAIN, format_mac(self.flic_hub.wifi.mac)))

return {
"identifiers": identifiers,
"name": "FlicHub",
"model": "LR",
"connections": connections,
"manufacturer": "Flic"
}
Expand Down
3 changes: 2 additions & 1 deletion custom_components/flichub/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
},
"abort": {
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]"
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]",
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
},
"options": {
Expand Down
3 changes: 2 additions & 1 deletion custom_components/flichub/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"cannot_connect": "Unable to connect to device"
},
"abort": {
"single_instance_allowed": "Only a single instance is allowed."
"single_instance_allowed": "Only a single instance is allowed.",
"already_configured": "Device is already configured"
}
},
"options": {
Expand Down

0 comments on commit 48ab6f3

Please sign in to comment.