Skip to content

Commit

Permalink
Add options flow to Withings (home-assistant#100300)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Sep 13, 2023
1 parent 6057fe5 commit f6b094d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
32 changes: 31 additions & 1 deletion homeassistant/components/withings/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
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

Expand All @@ -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."""
Expand Down Expand Up @@ -77,3 +87,23 @@ async def async_oauth_create_entry(self, data: dict[str, Any]) -> FlowResult:
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,
),
)
9 changes: 9 additions & 0 deletions homeassistant/components/withings/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
"default": "Successfully authenticated with Withings."
}
},
"options": {
"step": {
"init": {
"data": {
"use_webhook": "Use webhooks"
}
}
}
},
"entity": {
"binary_sensor": {
"in_bed": {
Expand Down
30 changes: 29 additions & 1 deletion tests/components/withings/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}

0 comments on commit f6b094d

Please sign in to comment.