Add options flow to Withings (#100300)
parent
6057fe5926
commit
f6b094dfee
|
@ -5,10 +5,12 @@ from collections.abc import Mapping
|
|||
import logging
|
||||
from typing import Any
|
||||
|
||||
import voluptuous as vol
|
||||
from withings_api.common import AuthScope
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.config_entries import ConfigEntry, OptionsFlowWithConfigEntry
|
||||
from homeassistant.const import CONF_TOKEN
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
from homeassistant.helpers import config_entry_oauth2_flow
|
||||
|
||||
|
@ -24,6 +26,14 @@ class WithingsFlowHandler(
|
|||
|
||||
reauth_entry: ConfigEntry | None = None
|
||||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(
|
||||
config_entry: ConfigEntry,
|
||||
) -> WithingsOptionsFlowHandler:
|
||||
"""Get the options flow for this handler."""
|
||||
return WithingsOptionsFlowHandler(config_entry)
|
||||
|
||||
@property
|
||||
def logger(self) -> logging.Logger:
|
||||
"""Return logger."""
|
||||
|
@ -77,3 +87,23 @@ class WithingsFlowHandler(
|
|||
return self.async_abort(reason="reauth_successful")
|
||||
|
||||
return self.async_abort(reason="wrong_account")
|
||||
|
||||
|
||||
class WithingsOptionsFlowHandler(OptionsFlowWithConfigEntry):
|
||||
"""Withings Options flow handler."""
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
"""Initialize form."""
|
||||
if user_input is not None:
|
||||
return self.async_create_entry(
|
||||
data=user_input,
|
||||
)
|
||||
return self.async_show_form(
|
||||
step_id="init",
|
||||
data_schema=self.add_suggested_values_to_schema(
|
||||
vol.Schema({vol.Required(CONF_USE_WEBHOOK): bool}),
|
||||
self.options,
|
||||
),
|
||||
)
|
||||
|
|
|
@ -28,6 +28,15 @@
|
|||
"default": "Successfully authenticated with Withings."
|
||||
}
|
||||
},
|
||||
"options": {
|
||||
"step": {
|
||||
"init": {
|
||||
"data": {
|
||||
"use_webhook": "Use webhooks"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"in_bed": {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Tests for config flow."""
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from homeassistant.components.withings.const import DOMAIN
|
||||
from homeassistant.components.withings.const import CONF_USE_WEBHOOK, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_REAUTH, SOURCE_USER
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import FlowResultType
|
||||
|
@ -255,3 +255,31 @@ async def test_config_reauth_wrong_account(
|
|||
assert result
|
||||
assert result["type"] == FlowResultType.ABORT
|
||||
assert result["reason"] == "wrong_account"
|
||||
|
||||
|
||||
async def test_options_flow(
|
||||
hass: HomeAssistant,
|
||||
hass_client_no_auth: ClientSessionGenerator,
|
||||
aioclient_mock: AiohttpClientMocker,
|
||||
config_entry: MockConfigEntry,
|
||||
withings: AsyncMock,
|
||||
disable_webhook_delay,
|
||||
current_request_with_host,
|
||||
) -> None:
|
||||
"""Test options flow."""
|
||||
await setup_integration(hass, config_entry)
|
||||
|
||||
result = await hass.config_entries.options.async_init(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.FORM
|
||||
assert result["step_id"] == "init"
|
||||
|
||||
result = await hass.config_entries.options.async_configure(
|
||||
result["flow_id"],
|
||||
user_input={CONF_USE_WEBHOOK: True},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert result["type"] == FlowResultType.CREATE_ENTRY
|
||||
assert result["data"] == {CONF_USE_WEBHOOK: True}
|
||||
|
|
Loading…
Reference in New Issue