2022-03-29 12:46:17 +00:00
|
|
|
"""Config flow for Utility Meter integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Mapping
|
|
|
|
from typing import Any, cast
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2022-04-11 07:20:56 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2022-03-29 12:46:17 +00:00
|
|
|
from homeassistant.helpers import selector
|
2022-03-30 21:36:47 +00:00
|
|
|
from homeassistant.helpers.schema_config_entry_flow import (
|
|
|
|
SchemaConfigFlowHandler,
|
|
|
|
SchemaFlowError,
|
|
|
|
SchemaFlowFormStep,
|
2022-03-29 12:46:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
BIMONTHLY,
|
|
|
|
CONF_METER_DELTA_VALUES,
|
|
|
|
CONF_METER_NET_CONSUMPTION,
|
|
|
|
CONF_METER_OFFSET,
|
|
|
|
CONF_METER_TYPE,
|
|
|
|
CONF_SOURCE_SENSOR,
|
|
|
|
CONF_TARIFFS,
|
|
|
|
DAILY,
|
|
|
|
DOMAIN,
|
|
|
|
HOURLY,
|
|
|
|
MONTHLY,
|
|
|
|
QUARTER_HOURLY,
|
|
|
|
QUARTERLY,
|
|
|
|
WEEKLY,
|
|
|
|
YEARLY,
|
|
|
|
)
|
|
|
|
|
|
|
|
METER_TYPES = [
|
2022-04-11 07:20:56 +00:00
|
|
|
selector.SelectOptionDict(value="none", label="No cycle"),
|
|
|
|
selector.SelectOptionDict(value=QUARTER_HOURLY, label="Every 15 minutes"),
|
|
|
|
selector.SelectOptionDict(value=HOURLY, label="Hourly"),
|
|
|
|
selector.SelectOptionDict(value=DAILY, label="Daily"),
|
|
|
|
selector.SelectOptionDict(value=WEEKLY, label="Weekly"),
|
|
|
|
selector.SelectOptionDict(value=MONTHLY, label="Monthly"),
|
|
|
|
selector.SelectOptionDict(value=BIMONTHLY, label="Every two months"),
|
|
|
|
selector.SelectOptionDict(value=QUARTERLY, label="Quarterly"),
|
|
|
|
selector.SelectOptionDict(value=YEARLY, label="Yearly"),
|
2022-03-29 12:46:17 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def _validate_config(data: Any) -> Any:
|
|
|
|
"""Validate config."""
|
|
|
|
try:
|
2022-03-31 11:57:26 +00:00
|
|
|
vol.Unique()(data[CONF_TARIFFS])
|
2022-03-29 12:46:17 +00:00
|
|
|
except vol.Invalid as exc:
|
2022-03-30 21:36:47 +00:00
|
|
|
raise SchemaFlowError("tariffs_not_unique") from exc
|
2022-03-29 12:46:17 +00:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
OPTIONS_SCHEMA = vol.Schema(
|
|
|
|
{
|
2022-04-11 07:20:56 +00:00
|
|
|
vol.Required(CONF_SOURCE_SENSOR): selector.EntitySelector(
|
|
|
|
selector.EntitySelectorConfig(domain="sensor"),
|
2022-03-29 12:46:17 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
2022-04-11 07:20:56 +00:00
|
|
|
vol.Required(CONF_NAME): selector.TextSelector(),
|
|
|
|
vol.Required(CONF_SOURCE_SENSOR): selector.EntitySelector(
|
|
|
|
selector.EntitySelectorConfig(domain="sensor"),
|
2022-03-29 12:46:17 +00:00
|
|
|
),
|
2022-04-11 07:20:56 +00:00
|
|
|
vol.Required(CONF_METER_TYPE): selector.SelectSelector(
|
|
|
|
selector.SelectSelectorConfig(options=METER_TYPES),
|
2022-03-29 12:46:17 +00:00
|
|
|
),
|
2022-04-11 07:20:56 +00:00
|
|
|
vol.Required(CONF_METER_OFFSET, default=0): selector.NumberSelector(
|
|
|
|
selector.NumberSelectorConfig(
|
|
|
|
min=0,
|
|
|
|
max=28,
|
|
|
|
mode=selector.NumberSelectorMode.BOX,
|
|
|
|
unit_of_measurement="days",
|
|
|
|
),
|
2022-03-29 12:46:17 +00:00
|
|
|
),
|
2022-04-11 07:20:56 +00:00
|
|
|
vol.Required(CONF_TARIFFS, default=[]): selector.SelectSelector(
|
|
|
|
selector.SelectSelectorConfig(options=[], custom_value=True, multiple=True),
|
2022-03-29 12:46:17 +00:00
|
|
|
),
|
2022-04-11 07:20:56 +00:00
|
|
|
vol.Required(
|
|
|
|
CONF_METER_NET_CONSUMPTION, default=False
|
|
|
|
): selector.BooleanSelector(),
|
|
|
|
vol.Required(
|
|
|
|
CONF_METER_DELTA_VALUES, default=False
|
|
|
|
): selector.BooleanSelector(),
|
2022-03-29 12:46:17 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-11-24 20:59:41 +00:00
|
|
|
CONFIG_FLOW = {
|
2022-03-30 21:36:47 +00:00
|
|
|
"user": SchemaFlowFormStep(CONFIG_SCHEMA, validate_user_input=_validate_config)
|
2022-03-29 12:46:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 20:59:41 +00:00
|
|
|
OPTIONS_FLOW = {
|
|
|
|
"init": SchemaFlowFormStep(OPTIONS_SCHEMA),
|
2022-03-29 12:46:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-30 21:36:47 +00:00
|
|
|
class ConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN):
|
2022-03-29 12:46:17 +00:00
|
|
|
"""Handle a config or options flow for Utility Meter."""
|
|
|
|
|
|
|
|
config_flow = CONFIG_FLOW
|
|
|
|
options_flow = OPTIONS_FLOW
|
|
|
|
|
|
|
|
def async_config_entry_title(self, options: Mapping[str, Any]) -> str:
|
|
|
|
"""Return config entry title."""
|
|
|
|
|
|
|
|
return cast(str, options[CONF_NAME])
|