Skip to content

Commit

Permalink
Update DHCP flows
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrayner committed Jan 10, 2023
1 parent f3a558e commit a827bc8
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 81 deletions.
90 changes: 29 additions & 61 deletions custom_components/pod_point/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
"""Adds config flow for Pod Point."""
from typing import Any
import logging
from typing import Any, Dict
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.components import dhcp
from homeassistant.data_entry_flow import FlowResult
from homeassistant.const import (
CONF_HOST,
CONF_MAC,
CONF_NAME,
)
from homeassistant.helpers.device_registry import format_mac
import voluptuous as vol

from podpointclient.client import PodPointClient
Expand All @@ -27,6 +24,8 @@
DEFAULT_CURRENCY,
)

_LOGGER = logging.getLogger(__name__)


class PodPointFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Pod Point."""
Expand All @@ -39,11 +38,13 @@ def __init__(self):
self._errors = {}

# pylint: disable=unused-argument
async def async_step_reauth(self, user_input=None):
async def async_step_reauth(self, user_input: Dict[str, str] = None) -> FlowResult:
"""Perform reauth upon an API authentication error."""
return await self.async_step_reauth_confirm()

async def async_step_reauth_confirm(self, user_input=None):
async def async_step_reauth_confirm(
self, user_input: Dict[str, str] = None
) -> FlowResult:
"""Dialog that informs the user that reauth is required."""
if user_input is None:
return self.async_show_form(
Expand All @@ -52,14 +53,10 @@ async def async_step_reauth_confirm(self, user_input=None):
)
return await self.async_step_user()

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

# Uncomment the next 2 lines if only a single instance of the integration is allowed:
# if self._async_current_entries():
# return self.async_abort(reason="single_instance_allowed")

if user_input is None:
user_input = {}
# Provide defaults for form
Expand Down Expand Up @@ -90,20 +87,24 @@ async def async_step_user(self, user_input=None):

@staticmethod
@callback
def async_get_options_flow(config_entry):
def async_get_options_flow(config_entry) -> FlowResult:
return PodPointOptionsFlowHandler(config_entry)

async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Prepare configuration for a DHCP discovered PodPoint device."""
return await self._process_discovered_device(
{
CONF_HOST: discovery_info.ip,
CONF_MAC: discovery_info.macaddress,
CONF_NAME: discovery_info.hostname,
}
)
formatted_mac = format_mac(discovery_info.macaddress)
_LOGGER.info("Found PodPoint device with mac %s", formatted_mac)

await self.async_set_unique_id(formatted_mac)
self._abort_if_unique_id_configured()

if self._async_current_entries():
return self.async_abort(reason="already_configured")

async def _show_config_form(self, user_input): # pylint: disable=unused-argument
return await self.async_step_user()

async def _show_config_form(
self, user_input: Dict[str, str]
) -> FlowResult: # pylint: disable=unused-argument
"""Show the configuration form to edit location data."""
return self.async_show_form(
step_id="user",
Expand All @@ -116,7 +117,7 @@ async def _show_config_form(self, user_input): # pylint: disable=unused-argumen
errors=self._errors,
)

async def _test_credentials(self, username, password):
async def _test_credentials(self, username: str, password: str) -> bool:
"""Return true if credentials is valid."""
try:
session = async_create_clientsession(self.hass)
Expand All @@ -128,41 +129,6 @@ async def _test_credentials(self, username, password):
pass
return False

async def _process_discovered_device(self, device: dict[str, Any]) -> FlowResult:
"""Prepare configuration for a discovered Axis device."""
# if device[CONF_MAC][:8] not in AXIS_OUI:
# return self.async_abort(reason="not_axis_device")

# if is_link_local(ip_address(device[CONF_HOST])):
# return self.async_abort(reason="link_local_address")

# await self.async_set_unique_id(device[CONF_MAC])

# self._abort_if_unique_id_configured(
# updates={
# CONF_HOST: device[CONF_HOST],
# CONF_PORT: device[CONF_PORT],
# }
# )

# self.context.update(
# {
# "title_placeholders": {
# CONF_NAME: device[CONF_NAME],
# CONF_HOST: device[CONF_HOST],
# },
# "configuration_url": f"http://{device[CONF_HOST]}:{device[CONF_PORT]}",
# }
# )

self.discovery_schema = {
vol.Required(CONF_HOST, default=device[CONF_HOST]): str,
vol.Required(CONF_EMAIL): str,
vol.Required(CONF_PASSWORD): str,
}

return await self.async_step_user()


class PodPointOptionsFlowHandler(config_entries.OptionsFlow):
"""Pod Point config flow options handler."""
Expand All @@ -172,11 +138,13 @@ def __init__(self, config_entry):
self.config_entry = config_entry
self.options = dict(config_entry.options)

async def async_step_init(self, user_input=None): # pylint: disable=unused-argument
async def async_step_init(
self, _=None
) -> FlowResult: # pylint: disable=unused-argument
"""Manage the options."""
return await self.async_step_user()

async def async_step_user(self, user_input=None):
async def async_step_user(self, user_input=None) -> FlowResult:
"""Handle a flow initialized by the user."""
if user_input is not None:
self.options.update(user_input)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/pod_point/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"@mattrayner"
],
"requirements": [
"podpointclient==1.1.0-alpha1",
"podpointclient==1.1.0",
"StrEnum>=0.4,<0.5"
],
"quality_scale": "gold",
Expand Down
5 changes: 0 additions & 5 deletions custom_components/pod_point/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@
"connected-waiting-for-schedule": "Connected but waiting for schedule"
}
}
},
"update": {
"firmware_update": {
"release_notes": "Test test"
}
}
}
}
12 changes: 0 additions & 12 deletions custom_components/pod_point/translations/sensor.en.json

This file was deleted.

2 changes: 1 addition & 1 deletion custom_components/pod_point/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""Version of Pod Point integration"""
__version__ = "0.5.0-alpha4"
__version__ = "0.5.0-alpha5"
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
podpointclient==1.1.0-alpha1
podpointclient==1.1.0
StrEnum>=0.4<0.5
6 changes: 6 additions & 0 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,9 @@ async def test_options_flow(hass):
CONF_HTTP_DEBUG: False,
CONF_CURRENCY: "GBP",
}


# Our config flow also has an DHCP flow, so we must test it as well.
@pytest.mark.asyncio
async def test_dhcp_flow(hass):
assert False is True
Empty file added tests/test_repair.py
Empty file.
Empty file added tests/test_update.py
Empty file.

0 comments on commit a827bc8

Please sign in to comment.