-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from mr-raw/v0.1.1
V0.1.1
- Loading branch information
Showing
11 changed files
with
130 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,67 @@ | ||
"""config_flow.py""" | ||
from homeassistant import config_entries | ||
from homeassistant.helpers.aiohttp_client import async_create_clientsession | ||
from __future__ import annotations | ||
|
||
import voluptuous as vol | ||
from .hra_api import ApiClient | ||
from .const import CONF_ADDRESS, DOMAIN | ||
from homeassistant import config_entries | ||
from homeassistant.helpers import selector | ||
|
||
from .const import CONF_ADDRESS, DOMAIN, LOGGER | ||
from .hra_api import ( | ||
ApiClientNoPickupDataFound, | ||
ApiClientCommunicationError, | ||
ApiClientError, | ||
HraApiClient, | ||
) | ||
|
||
|
||
class HRAConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): | ||
"""Config flow for HRA Recycling.""" | ||
|
||
VERSION = 1 | ||
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL | ||
|
||
def __init__(self): | ||
"""Initialize.""" | ||
self._errors = {} | ||
self.api_client = None | ||
|
||
async def async_step_user(self, user_input=None): | ||
async def async_step_user( | ||
self, | ||
user_input: dict | None = None, | ||
) -> config_entries.FlowResult: | ||
"""Handle a flow initialized by the user.""" | ||
self._errors = {} | ||
|
||
_errors = {} | ||
if user_input is not None: | ||
# Check if the address is correct here | ||
valid = await self._check_if_address_is_correct(user_input[CONF_ADDRESS]) | ||
if valid: | ||
try: | ||
await self._check_if_address_is_correct( | ||
address=user_input[CONF_ADDRESS] | ||
) | ||
except ApiClientNoPickupDataFound as exception: | ||
LOGGER.warning(exception) | ||
_errors["base"] = "invalid_address" | ||
except ApiClientCommunicationError as exception: | ||
LOGGER.warning(exception) | ||
_errors["base"] = "comm_error" | ||
except ApiClientError as exception: | ||
LOGGER.error(exception) | ||
_errors["base"] = "unknown_error" | ||
else: | ||
return self.async_create_entry( | ||
title=self.api_client.address, | ||
data={ | ||
"address": self.api_client.address, | ||
"agreement_id": self.api_client.agreement_id, | ||
}, | ||
title=user_input[CONF_ADDRESS], data=user_input | ||
) | ||
self._errors["base"] = "invalid_address" | ||
|
||
return await self._show_config_form(user_input) | ||
|
||
user_input = {} | ||
# Provide defaults for form | ||
user_input[CONF_ADDRESS] = "Rådhusvegen 39" | ||
|
||
return await self._show_config_form(user_input) | ||
|
||
async def _check_if_address_is_correct(self, address): | ||
"""Return true if address is valid.""" | ||
try: | ||
session = async_create_clientsession(self.hass) | ||
client = ApiClient(address, session) | ||
temp = await client.async_verify_address() | ||
# Original code: | ||
# if len(temp) == 1: | ||
# client.agreement_id = temp[0].get("agreementGuid") | ||
# self.api_client = client | ||
# return True | ||
# TODO: This does not check how many results, only return the first. | ||
# We should probably do some more with this. Maybe show the results and make | ||
# the user choose the correct one. | ||
client.agreement_id = temp[0].get("agreementGuid") | ||
self.api_client = client | ||
return True | ||
except Exception: # pylint: disable=broad-except | ||
pass | ||
return False | ||
|
||
async def _show_config_form(self, user_input): # pylint: disable=unused-argument | ||
"""Show the configuration form to edit location data.""" | ||
scheme = vol.Schema( | ||
{vol.Required(CONF_ADDRESS, default=user_input[CONF_ADDRESS]): str} | ||
) | ||
|
||
return self.async_show_form( | ||
step_id="user", data_schema=scheme, errors=self._errors | ||
step_id="user", | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required( | ||
CONF_ADDRESS, | ||
default=(user_input or {}).get(CONF_ADDRESS), | ||
): selector.TextSelector( | ||
selector.TextSelectorConfig( | ||
type=selector.TextSelectorType.TEXT | ||
), | ||
), | ||
} | ||
), | ||
errors=_errors, | ||
) | ||
|
||
async def _check_if_address_is_correct(self, address): | ||
"""Checks if the provided address is correct.""" | ||
client = HraApiClient(address=address) | ||
await client.async_verify_address() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.