mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Use reauth helpers in lametric (#128656)
This commit is contained in:
parent
099a3f4f90
commit
e0a14cdeea
@ -29,7 +29,7 @@ from homeassistant.components.ssdp import (
|
|||||||
ATTR_UPNP_SERIAL,
|
ATTR_UPNP_SERIAL,
|
||||||
SsdpServiceInfo,
|
SsdpServiceInfo,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry, ConfigFlowResult
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlowResult
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_DEVICE, CONF_HOST, CONF_MAC
|
from homeassistant.const import CONF_API_KEY, CONF_DEVICE, CONF_HOST, CONF_MAC
|
||||||
from homeassistant.data_entry_flow import AbortFlow
|
from homeassistant.data_entry_flow import AbortFlow
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
@ -59,7 +59,6 @@ class LaMetricFlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
|
|||||||
discovered_host: str
|
discovered_host: str
|
||||||
discovered_serial: str
|
discovered_serial: str
|
||||||
discovered: bool = False
|
discovered: bool = False
|
||||||
reauth_entry: ConfigEntry | None = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def logger(self) -> logging.Logger:
|
def logger(self) -> logging.Logger:
|
||||||
@ -113,9 +112,6 @@ class LaMetricFlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
|
|||||||
self, entry_data: Mapping[str, Any]
|
self, entry_data: Mapping[str, Any]
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle initiation of re-authentication with LaMetric."""
|
"""Handle initiation of re-authentication with LaMetric."""
|
||||||
self.reauth_entry = self.hass.config_entries.async_get_entry(
|
|
||||||
self.context["entry_id"]
|
|
||||||
)
|
|
||||||
return await self.async_step_choice_enter_manual_or_fetch_cloud()
|
return await self.async_step_choice_enter_manual_or_fetch_cloud()
|
||||||
|
|
||||||
async def async_step_choice_enter_manual_or_fetch_cloud(
|
async def async_step_choice_enter_manual_or_fetch_cloud(
|
||||||
@ -138,8 +134,8 @@ class LaMetricFlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
|
|||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
if self.discovered:
|
if self.discovered:
|
||||||
host = self.discovered_host
|
host = self.discovered_host
|
||||||
elif self.reauth_entry:
|
elif self.source == SOURCE_REAUTH:
|
||||||
host = self.reauth_entry.data[CONF_HOST]
|
host = self._get_reauth_entry().data[CONF_HOST]
|
||||||
else:
|
else:
|
||||||
host = user_input[CONF_HOST]
|
host = user_input[CONF_HOST]
|
||||||
|
|
||||||
@ -162,7 +158,7 @@ class LaMetricFlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
|
|||||||
TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
TextSelectorConfig(type=TextSelectorType.PASSWORD)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if not self.discovered and not self.reauth_entry:
|
if not self.discovered and self.source != SOURCE_REAUTH:
|
||||||
schema = {vol.Required(CONF_HOST): TextSelector()} | schema
|
schema = {vol.Required(CONF_HOST): TextSelector()} | schema
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
@ -195,10 +191,11 @@ class LaMetricFlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
|
|||||||
"""Handle device selection from devices offered by the cloud."""
|
"""Handle device selection from devices offered by the cloud."""
|
||||||
if self.discovered:
|
if self.discovered:
|
||||||
user_input = {CONF_DEVICE: self.discovered_serial}
|
user_input = {CONF_DEVICE: self.discovered_serial}
|
||||||
elif self.reauth_entry:
|
elif self.source == SOURCE_REAUTH:
|
||||||
if self.reauth_entry.unique_id not in self.devices:
|
reauth_unique_id = self._get_reauth_entry().unique_id
|
||||||
|
if reauth_unique_id not in self.devices:
|
||||||
return self.async_abort(reason="reauth_device_not_found")
|
return self.async_abort(reason="reauth_device_not_found")
|
||||||
user_input = {CONF_DEVICE: self.reauth_entry.unique_id}
|
user_input = {CONF_DEVICE: reauth_unique_id}
|
||||||
elif len(self.devices) == 1:
|
elif len(self.devices) == 1:
|
||||||
user_input = {CONF_DEVICE: list(self.devices.values())[0].serial_number}
|
user_input = {CONF_DEVICE: list(self.devices.values())[0].serial_number}
|
||||||
|
|
||||||
@ -251,7 +248,7 @@ class LaMetricFlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
|
|||||||
|
|
||||||
device = await lametric.device()
|
device = await lametric.device()
|
||||||
|
|
||||||
if not self.reauth_entry:
|
if self.source != SOURCE_REAUTH:
|
||||||
await self.async_set_unique_id(device.serial_number)
|
await self.async_set_unique_id(device.serial_number)
|
||||||
self._abort_if_unique_id_configured(
|
self._abort_if_unique_id_configured(
|
||||||
updates={CONF_HOST: lametric.host, CONF_API_KEY: lametric.api_key}
|
updates={CONF_HOST: lametric.host, CONF_API_KEY: lametric.api_key}
|
||||||
@ -273,19 +270,14 @@ class LaMetricFlowHandler(AbstractOAuth2FlowHandler, domain=DOMAIN):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if self.reauth_entry:
|
if self.source == SOURCE_REAUTH:
|
||||||
self.hass.config_entries.async_update_entry(
|
return self.async_update_reload_and_abort(
|
||||||
self.reauth_entry,
|
self._get_reauth_entry(),
|
||||||
data={
|
data_updates={
|
||||||
**self.reauth_entry.data,
|
|
||||||
CONF_HOST: lametric.host,
|
CONF_HOST: lametric.host,
|
||||||
CONF_API_KEY: lametric.api_key,
|
CONF_API_KEY: lametric.api_key,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
self.hass.async_create_task(
|
|
||||||
self.hass.config_entries.async_reload(self.reauth_entry.entry_id)
|
|
||||||
)
|
|
||||||
return self.async_abort(reason="reauth_successful")
|
|
||||||
|
|
||||||
return self.async_create_entry(
|
return self.async_create_entry(
|
||||||
title=device.name,
|
title=device.name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user