-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from robert-alfaro/feature/config-flow-support
Add Config Flow support
- Loading branch information
Showing
9 changed files
with
346 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,60 @@ | ||
"""Integration with Pool Math by Trouble Free Pool""" | ||
|
||
import logging | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_NAME, Platform | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import ConfigEntryNotReady | ||
|
||
from .const import CONF_SHARE_ID, CONF_TARGET, CONF_TIMEOUT, DOMAIN | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Set up Pool Math from a config entry.""" | ||
|
||
# prefer options | ||
share_id = entry.options.get(CONF_SHARE_ID, entry.data[CONF_SHARE_ID]) | ||
name = entry.options.get(CONF_NAME, entry.data[CONF_NAME]) | ||
timeout = entry.options.get(CONF_TIMEOUT, entry.data[CONF_TIMEOUT]) | ||
target = entry.options.get(CONF_TARGET, entry.data[CONF_TARGET]) | ||
|
||
# store options | ||
hass.config_entries.async_update_entry( | ||
entry, | ||
options={ | ||
CONF_SHARE_ID: share_id, | ||
CONF_NAME: name, | ||
CONF_TIMEOUT: timeout, | ||
CONF_TARGET: target, | ||
}, | ||
) | ||
|
||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {} | ||
|
||
# listen for options updates | ||
entry.async_on_unload(entry.add_update_listener(async_reload_entry)) | ||
|
||
# forward entry setup to platform(s) | ||
await hass.config_entries.async_forward_entry_setup(entry, Platform.SENSOR) | ||
|
||
return True | ||
|
||
|
||
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: | ||
"""Handle an options update.""" | ||
await hass.config_entries.async_reload(entry.entry_id) | ||
|
||
|
||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: | ||
"""Unload a Pool Math config entry.""" | ||
|
||
unload_ok = await hass.config_entries.async_unload_platforms( | ||
entry, [Platform.SENSOR] | ||
) | ||
if unload_ok: | ||
hass.data[DOMAIN].pop(entry.entry_id) | ||
|
||
return unload_ok |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
"""Config flow for Pool Math integration.""" | ||
|
||
import logging | ||
from typing import Any, Union | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.config_entries import ( | ||
ConfigEntry, | ||
ConfigFlow, | ||
OptionsFlowWithConfigEntry, | ||
) | ||
from homeassistant.const import CONF_NAME | ||
from homeassistant.core import callback | ||
from homeassistant.data_entry_flow import FlowResult | ||
from homeassistant.helpers import config_validation as cv | ||
|
||
from .const import ( | ||
CONF_SHARE_ID, | ||
CONF_TARGET, | ||
CONF_TIMEOUT, | ||
DEFAULT_NAME, | ||
DEFAULT_TARGET, | ||
DEFAULT_TIMEOUT, | ||
DOMAIN, | ||
INTEGRATION_NAME, | ||
) | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
def _initial_form(flow: Union[ConfigFlow, OptionsFlowWithConfigEntry]): | ||
"""Return flow form for init/user step id.""" | ||
|
||
if isinstance(flow, ConfigFlow): | ||
step_id = "user" | ||
share_id = None | ||
name = DEFAULT_NAME | ||
timeout = DEFAULT_TIMEOUT | ||
target = DEFAULT_TARGET | ||
elif isinstance(flow, OptionsFlowWithConfigEntry): | ||
step_id = "init" | ||
share_id = flow.config_entry.options.get(CONF_SHARE_ID) | ||
name = flow.config_entry.options.get(CONF_NAME, DEFAULT_NAME) | ||
timeout = flow.config_entry.options.get(CONF_TIMEOUT, DEFAULT_TIMEOUT) | ||
target = flow.config_entry.options.get(CONF_TARGET, DEFAULT_TARGET) | ||
else: | ||
raise TypeError("Invalid flow type") | ||
|
||
return flow.async_show_form( | ||
step_id=step_id, # parameterized to follow guidance on using "user" | ||
data_schema=vol.Schema( | ||
{ | ||
vol.Required(CONF_SHARE_ID, default=share_id): cv.string, | ||
vol.Optional(CONF_NAME, default=name): cv.string, | ||
vol.Optional(CONF_TIMEOUT, default=timeout): cv.positive_int, | ||
# NOTE: targets are not really implemented, other than tfp | ||
vol.Optional( | ||
CONF_TARGET, default=target | ||
): cv.string, # targets/*.yaml file with min/max targets | ||
# FIXME: allow specifying EXACTLY which log types to monitor, always create the sensors | ||
# vol.Optional(CONF_LOG_TYPES, default=None): | ||
} | ||
), | ||
) | ||
|
||
|
||
class PoolMathOptionsFlow(OptionsFlowWithConfigEntry): | ||
"""Handle Pool Math options.""" | ||
|
||
async def async_step_init( | ||
self, user_input: dict[str, Any] | None = None | ||
) -> FlowResult: | ||
"""Manage Pool Math options.""" | ||
if user_input is not None: | ||
return self.async_create_entry(title=INTEGRATION_NAME, data=user_input) | ||
|
||
return _initial_form(self) | ||
|
||
|
||
class PoolMathFlowHandler(ConfigFlow, domain=DOMAIN): | ||
"""Handle a Pool Math config flow.""" | ||
|
||
async def async_step_user(self, user_input=None) -> FlowResult: | ||
"""Handle the initial step.""" | ||
if user_input is not None: | ||
# already configured share_id? | ||
share_id = user_input.get(CONF_SHARE_ID) | ||
await self.async_set_unique_id(share_id) | ||
self._abort_if_unique_id_configured() | ||
|
||
return self.async_create_entry(title=INTEGRATION_NAME, data=user_input) | ||
|
||
return _initial_form(self) | ||
|
||
@staticmethod | ||
@callback | ||
def async_get_options_flow( | ||
config_entry: ConfigEntry, | ||
) -> PoolMathOptionsFlow: | ||
"""Get the options flow for this handler.""" | ||
return PoolMathOptionsFlow(config_entry) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
"""Constants for Pool Math.""" | ||
|
||
DOMAIN = 'poolmath' | ||
INTEGRATION_NAME = "Pool Math" | ||
DOMAIN = "poolmath" | ||
|
||
ATTRIBUTION = 'Data by PoolMath (Trouble Free Pool)' | ||
ATTRIBUTION = "Data by PoolMath (Trouble Free Pool)" | ||
|
||
ATTR_ATTRIBUTION = 'attribution' | ||
ATTR_DESCRIPTION = 'description' | ||
ATTR_LAST_UPDATED_TIME = 'last_updated' | ||
ATTR_TARGET_MIN = 'target_min' | ||
ATTR_TARGET_MAX = 'target_max' | ||
ATTR_TARGET_SOURCE = 'target_source' | ||
ATTR_DESCRIPTION = "description" | ||
ATTR_LAST_UPDATED_TIME = "last_updated" | ||
ATTR_TARGET_MIN = "target_min" | ||
ATTR_TARGET_MAX = "target_max" | ||
ATTR_TARGET_SOURCE = "target_source" | ||
|
||
CONF_TARGET = 'target' | ||
CONF_TIMEOUT = 'timeout' | ||
CONF_SHARE_ID = "share_id" | ||
CONF_TARGET = "target" | ||
CONF_TIMEOUT = "timeout" | ||
|
||
DEFAULT_NAME = 'Pool' | ||
DEFAULT_NAME = "Pool" | ||
DEFAULT_TIMEOUT = 15.0 | ||
DEFAULT_TARGET = "tfp" | ||
|
||
ICON_GAUGE = 'mdi:gauge' | ||
ICON_POOL = 'mdi:pool' | ||
ICON_GAUGE = "mdi:gauge" | ||
ICON_POOL = "mdi:pool" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.