Skip to content

Commit

Permalink
Setup using config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
JohNan committed Feb 28, 2023
1 parent 5de9e0d commit 7b2f9d8
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 45 deletions.
6 changes: 0 additions & 6 deletions custom_components/flichub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@

_LOGGER: logging.Logger = logging.getLogger(__package__)


async def async_setup(hass: HomeAssistant, config: Config):
"""Set up this integration using YAML is not supported."""
return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""
if hass.data.get(DOMAIN) is None:
Expand Down
38 changes: 0 additions & 38 deletions custom_components/flichub/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
self._errors = {}

if self.ip_address is None:
response = await FlicHubTcpClient.discover(async_create_clientsession(self.hass))
if len(response) > 0:
self.discovered_hubs = response
return await self.async_step_devices(user_input)

if user_input is not None:
valid = await self._test_credentials(
user_input[CONF_IP_ADDRESS],
Expand All @@ -59,24 +53,6 @@ async def async_step_user(self, user_input=None):

return await self._show_config_form(user_input)

async def async_step_devices(self, user_input=None):
if user_input is not None:
valid = await self._test_credentials(
user_input[CONF_IP_ADDRESS],
user_input[CONF_PORT]
)
if valid:
user_input[CONF_HUB_SERIAL] = next(
hub for hub in self.discovered_hubs if hub['local_ip'] == user_input[CONF_IP_ADDRESS]
)
user_input[CONF_FIRMWARE_VERSION] = next(
hub['serial_number'] for hub in self.discovered_hubs if hub['local_ip'] == user_input[CONF_IP_ADDRESS]
)
return await self._create_entry(user_input)
self._errors["base"] = "auth"

return await self._show_select_device_form(self.discovered_hubs)

async def _create_entry(self, user_input):
existing_entry = await self.async_set_unique_id(
user_input[CONF_IP_ADDRESS]
Expand All @@ -102,20 +78,6 @@ async def _show_config_form(self, user_input): # pylint: disable=unused-argumen
errors=self._errors,
)

async def _show_select_device_form(self, discovered_hubs): # pylint: disable=unused-argument
"""Show the configuration form to edit location data."""
hubs = {hub['local_ip']: f"{hub['serial_number']} ({hub['local_ip']})" for hub in discovered_hubs}
return self.async_show_form(
step_id="devices",
data_schema=vol.Schema(
{
vol.Required(CONF_IP_ADDRESS): vol.In(hubs),
vol.Required(CONF_PORT): str
}
),
errors=self._errors,
)

async def _test_credentials(self, ip, port):
"""Return true if credentials is valid."""
try:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/flichub/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Flic Hub",
"documentation": "https://github.com/JohNan/home-assistant-flichub",
"issue_tracker": "https://github.com/JohNan/home-assistant-flichub/issues",
"config_flow": false,
"config_flow": true,
"codeowners": ["@JohNan"],
"requirements": ["pyflichub-tcpclient==0.1.2"],
"loggers": ["pyflichub"],
Expand Down

0 comments on commit 7b2f9d8

Please sign in to comment.