Add reauth support to doorbird (#121815)

This commit is contained in:
J. Nick Koston 2024-07-12 09:53:39 -05:00 committed by GitHub
parent 726fcb485d
commit a8321fac95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 135 additions and 38 deletions

View file

@ -2,6 +2,7 @@
from __future__ import annotations
from collections.abc import Mapping
from http import HTTPStatus
import logging
from typing import Any
@ -21,6 +22,7 @@ from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNA
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import VolDictType
from .const import (
CONF_EVENTS,
@ -36,14 +38,20 @@ _LOGGER = logging.getLogger(__name__)
DEFAULT_OPTIONS = {CONF_EVENTS: [DEFAULT_DOORBELL_EVENT, DEFAULT_MOTION_EVENT]}
AUTH_VOL_DICT: VolDictType = {
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
}
AUTH_SCHEMA = vol.Schema(AUTH_VOL_DICT)
def _schema_with_defaults(
host: str | None = None, name: str | None = None
) -> vol.Schema:
return vol.Schema(
{
vol.Required(CONF_HOST, default=host): str,
vol.Required(CONF_USERNAME): str,
vol.Required(CONF_PASSWORD): str,
**AUTH_VOL_DICT,
vol.Optional(CONF_NAME, default=name): str,
}
)
@ -56,7 +64,6 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
data[CONF_HOST], data[CONF_USERNAME], data[CONF_PASSWORD], http_session=session
)
try:
status = await device.ready()
info = await device.info()
except ClientResponseError as err:
if err.status == HTTPStatus.UNAUTHORIZED:
@ -65,9 +72,6 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
except OSError as err:
raise CannotConnect from err
if not status[0]:
raise CannotConnect
mac_addr = get_mac_address_from_door_station_info(info)
# Return info that you want to store in the config entry.
@ -96,6 +100,47 @@ class DoorBirdConfigFlow(ConfigFlow, domain=DOMAIN):
def __init__(self) -> None:
"""Initialize the DoorBird config flow."""
self.discovery_schema: vol.Schema | None = None
self.reauth_entry: ConfigEntry | None = None
async def async_step_reauth(
self, entry_data: Mapping[str, Any]
) -> ConfigFlowResult:
"""Handle reauth."""
entry_id = self.context["entry_id"]
self.reauth_entry = self.hass.config_entries.async_get_entry(entry_id)
return await self.async_step_reauth_confirm()
async def async_step_reauth_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle reauth input."""
errors: dict[str, str] = {}
existing_entry = self.reauth_entry
assert existing_entry
existing_data = existing_entry.data
placeholders: dict[str, str] = {
CONF_NAME: existing_data[CONF_NAME],
CONF_HOST: existing_data[CONF_HOST],
}
self.context["title_placeholders"] = placeholders
if user_input is not None:
new_config = {
**existing_data,
CONF_USERNAME: user_input[CONF_USERNAME],
CONF_PASSWORD: user_input[CONF_PASSWORD],
}
_, errors = await self._async_validate_or_error(new_config)
if not errors:
return self.async_update_reload_and_abort(
existing_entry, data=new_config
)
return self.async_show_form(
description_placeholders=placeholders,
step_id="reauth_confirm",
data_schema=AUTH_SCHEMA,
errors=errors,
)
async def async_step_user(
self, user_input: dict[str, Any] | None = None