Skip to content

Commit

Permalink
Add config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy committed Oct 16, 2024
1 parent 47efdfd commit fa8c1ee
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 69 deletions.
16 changes: 12 additions & 4 deletions custom_components/hass_proxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@

from typing import TYPE_CHECKING

from .const import LOGGER

if TYPE_CHECKING:
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant

from .data import IntegrationBlueprintConfigEntry
from .data import HASSProxyData

PLATFORMS: list[Platform] = []


# https://developers.home-assistant.io/docs/config_entries_index/#setting-up-an-entry
async def async_setup_entry(
hass: HomeAssistant,
entry: IntegrationBlueprintConfigEntry,
entry: HASSProxyData,
) -> bool:
"""Set up this integration."""
LOGGER.info("HASSPROXY Setting up entry %s", entry.entry_id)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(async_reload_entry))

Expand All @@ -32,16 +36,20 @@ async def async_setup_entry(

async def async_unload_entry(
hass: HomeAssistant,
entry: IntegrationBlueprintConfigEntry,
entry: HASSProxyData,
) -> bool:
"""Handle removal of an entry."""

LOGGER.info("HASSPROXY Unloading entry %s", entry.entry_id)
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


async def async_reload_entry(
hass: HomeAssistant,
entry: IntegrationBlueprintConfigEntry,
entry: HASSProxyData,
) -> None:
"""Reload config entry."""
LOGGER.info("HASSPROXY Reloading entry %s", entry.entry_id)

await async_unload_entry(hass, entry)
await async_setup_entry(hass, entry)
92 changes: 41 additions & 51 deletions custom_components/hass_proxy/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,71 @@
"""Adds config flow for Blueprint."""
A"""Config flow for HASS Proxy."""

from __future__ import annotations

import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.helpers import selector
from homeassistant.helpers.aiohttp_client import async_create_clientsession

from .api import (
IntegrationBlueprintApiClient,
IntegrationBlueprintApiClientAuthenticationError,
IntegrationBlueprintApiClientCommunicationError,
IntegrationBlueprintApiClientError,
)
from .const import DOMAIN, LOGGER
from .const import CONF_URLS, DOMAIN


class BlueprintFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Blueprint."""
class HASSProxyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): # type: ignore[call-arg,misc]
"""Config flow for HASS Proxy."""

VERSION = 1

@staticmethod
@callback # type: ignore[misc]
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> HASSProxyOptionsFlowHandler:
"""Get the Frigate Options flow."""
return HASSProxyOptionsFlowHandler(config_entry)

async def async_step_user(
self,
user_input: dict | None = None,
) -> data_entry_flow.FlowResult:
) -> config_entries.ConfigFlowResult:
"""Handle a flow initialized by the user."""
_errors = {}
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

return self.async_create_entry(title="HASS Proxy", data=user_input or {})


class HASSProxyOptionsFlowHandler(config_entries.OptionsFlow):
"""Options flow for Blueprint."""

VERSION = 1

def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
"""Initialize an options flow."""
self._config_entry = config_entry

async def async_step_init(
self,
user_input: dict | None = None,
) -> config_entries.ConfigFlowResult:
"""Manage the options."""
if user_input is not None:
try:
await self._test_credentials(
username=user_input[CONF_USERNAME],
password=user_input[CONF_PASSWORD],
)
except IntegrationBlueprintApiClientAuthenticationError as exception:
LOGGER.warning(exception)
_errors["base"] = "auth"
except IntegrationBlueprintApiClientCommunicationError as exception:
LOGGER.error(exception)
_errors["base"] = "connection"
except IntegrationBlueprintApiClientError as exception:
LOGGER.exception(exception)
_errors["base"] = "unknown"
else:
return self.async_create_entry(
title=user_input[CONF_USERNAME],
data=user_input,
)
return self.async_create_entry(
data=user_input,
)

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(
CONF_USERNAME,
default=(user_input or {}).get(CONF_USERNAME, vol.UNDEFINED),
CONF_URLS,
default=(user_input or {}).get(CONF_URLS, vol.UNDEFINED),
): selector.TextSelector(
selector.TextSelectorConfig(
type=selector.TextSelectorType.TEXT,
),
),
vol.Required(CONF_PASSWORD): selector.TextSelector(
selector.TextSelectorConfig(
type=selector.TextSelectorType.PASSWORD,
multiline=True,
),
),
},
),
errors=_errors,
)

async def _test_credentials(self, username: str, password: str) -> None:
"""Validate credentials."""
client = IntegrationBlueprintApiClient(
username=username,
password=password,
session=async_create_clientsession(self.hass),
)
await client.async_get_data()
6 changes: 6 additions & 0 deletions custom_components/hass_proxy/const.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""Constants for hass_proxy."""

from logging import Logger, getLogger

DOMAIN = "hass_proxy"

CONF_URLS = "urls"

LOGGER: Logger = getLogger(__package__)
29 changes: 15 additions & 14 deletions custom_components/hass_proxy/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
{
"config": {
"step": {
"user": {
"description": "If you need help with the configuration have a look here: https://github.com/ludeeus/integration_blueprint",
"data": {
"username": "Username",
"password": "Password"
}
}
},
"error": {
"auth": "Username/Password is wrong.",
"connection": "Unable to connect to the server.",
"unknown": "Unknown error occurred."
"config": {
"step": {
"user": {}
},
"abort": {
"single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]"
}
},
"options": {
"step": {
"user": {
"data": {
"urls": "Allowable URLs to proxy"
}
}
}
}
}

0 comments on commit fa8c1ee

Please sign in to comment.