Refactor onewire options flow (#87162)

* Refactor onewire options flow

* Adjust

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Use async_entries_for_config_entry

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
epenet 2023-02-05 01:18:20 +01:00 committed by GitHub
parent a703aa1dff
commit 2f3b509513
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 50 deletions

View File

@ -5,12 +5,16 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
OptionsFlowWithConfigEntry,
)
from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers import config_validation as cv, device_registry as dr
from homeassistant.helpers.device_registry import DeviceRegistry from homeassistant.helpers.device_registry import DeviceEntry
from .const import ( from .const import (
DEFAULT_HOST, DEFAULT_HOST,
@ -23,7 +27,6 @@ from .const import (
OPTION_ENTRY_SENSOR_PRECISION, OPTION_ENTRY_SENSOR_PRECISION,
PRECISION_MAPPING_FAMILY_28, PRECISION_MAPPING_FAMILY_28,
) )
from .model import OWDeviceDescription
from .onewirehub import CannotConnect, OneWireHub from .onewirehub import CannotConnect, OneWireHub
DATA_SCHEMA = vol.Schema( DATA_SCHEMA = vol.Schema(
@ -96,38 +99,45 @@ class OneWireFlowHandler(ConfigFlow, domain=DOMAIN):
@staticmethod @staticmethod
@callback @callback
def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow: def async_get_options_flow(config_entry: ConfigEntry) -> OnewireOptionsFlowHandler:
"""Get the options flow for this handler.""" """Get the options flow for this handler."""
return OnewireOptionsFlowHandler(config_entry) return OnewireOptionsFlowHandler(config_entry)
class OnewireOptionsFlowHandler(OptionsFlow): class OnewireOptionsFlowHandler(OptionsFlowWithConfigEntry):
"""Handle OneWire Config options.""" """Handle OneWire Config options."""
def __init__(self, config_entry: ConfigEntry) -> None: configurable_devices: dict[str, str]
"""Initialize OneWire Network options flow.""" """Mapping of the configurable devices.
self.entry_id = config_entry.entry_id
self.options = dict(config_entry.options) `key`: friendly name
self.configurable_devices: dict[str, OWDeviceDescription] = {} `value`: onewire id
self.devices_to_configure: dict[str, OWDeviceDescription] = {} """
self.current_device: str = "" devices_to_configure: dict[str, str]
"""Mapping of the devices selected for configuration.
`key`: friendly name
`value`: onewire id
"""
current_device: str
"""Friendly name of the currently selected device."""
async def async_step_init( async def async_step_init(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
) -> FlowResult: ) -> FlowResult:
"""Manage the options.""" """Manage the options."""
controller: OneWireHub = self.hass.data[DOMAIN][self.entry_id]
all_devices: list[OWDeviceDescription] = controller.devices # type: ignore[assignment]
if not all_devices:
return self.async_abort(reason="No configurable devices found.")
device_registry = dr.async_get(self.hass) device_registry = dr.async_get(self.hass)
self.configurable_devices = { self.configurable_devices = {
self._get_device_long_name(device_registry, device.id): device self._get_device_friendly_name(device, device.name): device.name
for device in all_devices for device in dr.async_entries_for_config_entry(
if device.family in DEVICE_SUPPORT_OPTIONS device_registry, self.config_entry.entry_id
)
if device.name and device.name[0:2] in DEVICE_SUPPORT_OPTIONS
} }
if not self.configurable_devices:
return self.async_abort(reason="No configurable devices found.")
return await self.async_step_device_selection(user_input=None) return await self.async_step_device_selection(user_input=None)
async def async_step_device_selection( async def async_step_device_selection(
@ -138,16 +148,15 @@ class OnewireOptionsFlowHandler(OptionsFlow):
if user_input is not None: if user_input is not None:
if user_input.get(INPUT_ENTRY_CLEAR_OPTIONS): if user_input.get(INPUT_ENTRY_CLEAR_OPTIONS):
# Reset all options # Reset all options
self.options = {} return self.async_create_entry(data={})
return self._async_update_options()
selected_devices: list[str] = ( selected_devices: list[str] = (
user_input.get(INPUT_ENTRY_DEVICE_SELECTION) or [] user_input.get(INPUT_ENTRY_DEVICE_SELECTION) or []
) )
if selected_devices: if selected_devices:
self.devices_to_configure = { self.devices_to_configure = {
device_name: self.configurable_devices[device_name] friendly_name: self.configurable_devices[friendly_name]
for device_name in selected_devices for friendly_name in selected_devices
} }
return await self.async_step_configure_device(user_input=None) return await self.async_step_configure_device(user_input=None)
@ -166,7 +175,10 @@ class OnewireOptionsFlowHandler(OptionsFlow):
default=self._get_current_configured_sensors(), default=self._get_current_configured_sensors(),
description="Multiselect with list of devices to choose from", description="Multiselect with list of devices to choose from",
): cv.multi_select( ): cv.multi_select(
{device: False for device in self.configurable_devices} {
friendly_name: False
for friendly_name in self.configurable_devices
}
), ),
} }
), ),
@ -181,15 +193,15 @@ class OnewireOptionsFlowHandler(OptionsFlow):
self._update_device_options(user_input) self._update_device_options(user_input)
if self.devices_to_configure: if self.devices_to_configure:
return await self.async_step_configure_device(user_input=None) return await self.async_step_configure_device(user_input=None)
return self._async_update_options() return self.async_create_entry(data=self.options)
self.current_device, description = self.devices_to_configure.popitem() self.current_device, onewire_id = self.devices_to_configure.popitem()
data_schema = vol.Schema( data_schema = vol.Schema(
{ {
vol.Required( vol.Required(
OPTION_ENTRY_SENSOR_PRECISION, OPTION_ENTRY_SENSOR_PRECISION,
default=self._get_current_setting( default=self._get_current_setting(
description.id, OPTION_ENTRY_SENSOR_PRECISION, "temperature" onewire_id, OPTION_ENTRY_SENSOR_PRECISION, "temperature"
), ),
): vol.In(PRECISION_MAPPING_FAMILY_28), ): vol.In(PRECISION_MAPPING_FAMILY_28),
} }
@ -201,19 +213,11 @@ class OnewireOptionsFlowHandler(OptionsFlow):
description_placeholders={"sensor_id": self.current_device}, description_placeholders={"sensor_id": self.current_device},
) )
@callback
def _async_update_options(self) -> FlowResult:
"""Update config entry options."""
return self.async_create_entry(title="", data=self.options)
@staticmethod @staticmethod
def _get_device_long_name( def _get_device_friendly_name(entry: DeviceEntry, onewire_id: str) -> str:
device_registry: DeviceRegistry, current_device: str if entry.name_by_user:
) -> str: return f"{entry.name_by_user} ({onewire_id})"
device = device_registry.async_get_device({(DOMAIN, current_device)}) return onewire_id
if device and device.name_by_user:
return f"{device.name_by_user} ({current_device})"
return current_device
def _get_current_configured_sensors(self) -> list[str]: def _get_current_configured_sensors(self) -> list[str]:
"""Get current list of sensors that are configured.""" """Get current list of sensors that are configured."""
@ -221,9 +225,9 @@ class OnewireOptionsFlowHandler(OptionsFlow):
if not configured_sensors: if not configured_sensors:
return [] return []
return [ return [
device_name friendly_name
for device_name, description in self.configurable_devices.items() for friendly_name, onewire_id in self.configurable_devices.items()
if description.id in configured_sensors if onewire_id in configured_sensors
] ]
def _get_current_setting(self, device_id: str, setting: str, default: Any) -> Any: def _get_current_setting(self, device_id: str, setting: str, default: Any) -> Any:
@ -239,9 +243,9 @@ class OnewireOptionsFlowHandler(OptionsFlow):
OPTION_ENTRY_DEVICE_OPTIONS, {} OPTION_ENTRY_DEVICE_OPTIONS, {}
) )
description = self.configurable_devices[self.current_device] onewire_id = self.configurable_devices[self.current_device]
device_options: dict[str, Any] = options.setdefault(description.id, {}) device_options: dict[str, Any] = options.setdefault(onewire_id, {})
if description.family == "28": if onewire_id[0:2] == "28":
device_options[OPTION_ENTRY_SENSOR_PRECISION] = user_input[ device_options[OPTION_ENTRY_SENSOR_PRECISION] = user_input[
OPTION_ENTRY_SENSOR_PRECISION OPTION_ENTRY_SENSOR_PRECISION
] ]

View File

@ -9,6 +9,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import device_registry as dr
from . import setup_owproxy_mock_devices from . import setup_owproxy_mock_devices
from .const import MOCK_OWPROXY_DEVICES from .const import MOCK_OWPROXY_DEVICES
@ -137,11 +138,12 @@ async def test_user_options_set_multiple(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify that first config step comes back with a selection list of all the 28-family devices # Verify that first config step comes back with a selection list of all the 28-family devices
with patch( device_registry = dr.async_get(hass)
"homeassistant.helpers.device_registry.DeviceRegistry.async_get_device", for entry in dr.async_entries_for_config_entry(
return_value=FakeDevice(), device_registry, config_entry.entry_id
): ):
result = await hass.config_entries.options.async_init(config_entry.entry_id) device_registry.async_update_device(entry.id, name_by_user="Given Name")
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["data_schema"].schema["device_selection"].options == { assert result["data_schema"].schema["device_selection"].options == {
"Given Name (28.111111111111)": False, "Given Name (28.111111111111)": False,
"Given Name (28.222222222222)": False, "Given Name (28.222222222222)": False,