forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config flow to generic hygrostat (home-assistant#119017)
* Add config flow to hygrostat Co-authored-by: Franck Nijhof <[email protected]>
- Loading branch information
Showing
9 changed files
with
400 additions
and
13 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
100 changes: 100 additions & 0 deletions
100
homeassistant/components/generic_hygrostat/config_flow.py
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,100 @@ | ||
"""Config flow for Generic hygrostat.""" | ||
|
||
from __future__ import annotations | ||
|
||
from collections.abc import Mapping | ||
from typing import Any, cast | ||
|
||
import voluptuous as vol | ||
|
||
from homeassistant.components.humidifier import HumidifierDeviceClass | ||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, SensorDeviceClass | ||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN | ||
from homeassistant.const import CONF_NAME, PERCENTAGE | ||
from homeassistant.helpers import selector | ||
from homeassistant.helpers.schema_config_entry_flow import ( | ||
SchemaConfigFlowHandler, | ||
SchemaFlowFormStep, | ||
) | ||
|
||
from . import ( | ||
CONF_DEVICE_CLASS, | ||
CONF_DRY_TOLERANCE, | ||
CONF_HUMIDIFIER, | ||
CONF_MIN_DUR, | ||
CONF_SENSOR, | ||
CONF_WET_TOLERANCE, | ||
DEFAULT_TOLERANCE, | ||
DOMAIN, | ||
) | ||
|
||
OPTIONS_SCHEMA = { | ||
vol.Required(CONF_DEVICE_CLASS): selector.SelectSelector( | ||
selector.SelectSelectorConfig( | ||
options=[ | ||
HumidifierDeviceClass.HUMIDIFIER, | ||
HumidifierDeviceClass.DEHUMIDIFIER, | ||
], | ||
translation_key=CONF_DEVICE_CLASS, | ||
mode=selector.SelectSelectorMode.DROPDOWN, | ||
), | ||
), | ||
vol.Required(CONF_SENSOR): selector.EntitySelector( | ||
selector.EntitySelectorConfig( | ||
domain=SENSOR_DOMAIN, device_class=SensorDeviceClass.HUMIDITY | ||
) | ||
), | ||
vol.Required(CONF_HUMIDIFIER): selector.EntitySelector( | ||
selector.EntitySelectorConfig(domain=SWITCH_DOMAIN) | ||
), | ||
vol.Required( | ||
CONF_DRY_TOLERANCE, default=DEFAULT_TOLERANCE | ||
): selector.NumberSelector( | ||
selector.NumberSelectorConfig( | ||
min=0, | ||
max=100, | ||
step=0.5, | ||
unit_of_measurement=PERCENTAGE, | ||
mode=selector.NumberSelectorMode.BOX, | ||
) | ||
), | ||
vol.Required( | ||
CONF_WET_TOLERANCE, default=DEFAULT_TOLERANCE | ||
): selector.NumberSelector( | ||
selector.NumberSelectorConfig( | ||
min=0, | ||
max=100, | ||
step=0.5, | ||
unit_of_measurement=PERCENTAGE, | ||
mode=selector.NumberSelectorMode.BOX, | ||
) | ||
), | ||
vol.Optional(CONF_MIN_DUR): selector.DurationSelector( | ||
selector.DurationSelectorConfig(allow_negative=False) | ||
), | ||
} | ||
|
||
CONFIG_SCHEMA = { | ||
vol.Required(CONF_NAME): selector.TextSelector(), | ||
**OPTIONS_SCHEMA, | ||
} | ||
|
||
|
||
CONFIG_FLOW = { | ||
"user": SchemaFlowFormStep(vol.Schema(CONFIG_SCHEMA)), | ||
} | ||
|
||
OPTIONS_FLOW = { | ||
"init": SchemaFlowFormStep(vol.Schema(OPTIONS_SCHEMA)), | ||
} | ||
|
||
|
||
class ConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN): | ||
"""Handle a config or options flow.""" | ||
|
||
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["name"]) |
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
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,56 @@ | ||
{ | ||
"title": "Generic hygrostat", | ||
"config": { | ||
"step": { | ||
"user": { | ||
"title": "Add generic hygrostat", | ||
"description": "Create a entity that control the humidity via a switch and sensor.", | ||
"data": { | ||
"device_class": "Device class", | ||
"dry_tolerance": "Dry tolerance", | ||
"humidifier": "Switch", | ||
"min_cycle_duration": "Minimum cycle duration", | ||
"name": "[%key:common::config_flow::data::name%]", | ||
"target_sensor": "Humidity sensor", | ||
"wet_tolerance": "Wet tolerance" | ||
}, | ||
"data_description": { | ||
"dry_tolerance": "The minimum amount of difference between the humidity read by the sensor specified in the target sensor option and the target humidity that must change prior to being switched on.", | ||
"humidifier": "Humidifier or dehumidifier switch; must be a toggle device.", | ||
"min_cycle_duration": "Set a minimum amount of time that the switch specified in the humidifier option must be in its current state prior to being switched either off or on.", | ||
"target_sensor": "Sensor with current humidity.", | ||
"wet_tolerance": "The minimum amount of difference between the humidity read by the sensor specified in the target sensor option and the target humidity that must change prior to being switched off." | ||
} | ||
} | ||
} | ||
}, | ||
"options": { | ||
"step": { | ||
"init": { | ||
"data": { | ||
"device_class": "[%key:component::generic_hygrostat::config::step::user::data::device_class%]", | ||
"dry_tolerance": "[%key:component::generic_hygrostat::config::step::user::data::dry_tolerance%]", | ||
"humidifier": "[%key:component::generic_hygrostat::config::step::user::data::humidifier%]", | ||
"min_cycle_duration": "[%key:component::generic_hygrostat::config::step::user::data::min_cycle_duration%]", | ||
"target_sensor": "[%key:component::generic_hygrostat::config::step::user::data::target_sensor%]", | ||
"wet_tolerance": "[%key:component::generic_hygrostat::config::step::user::data::wet_tolerance%]" | ||
}, | ||
"data_description": { | ||
"dry_tolerance": "[%key:component::generic_hygrostat::config::step::user::data_description::dry_tolerance%]", | ||
"humidifier": "[%key:component::generic_hygrostat::config::step::user::data_description::humidifier%]", | ||
"min_cycle_duration": "[%key:component::generic_hygrostat::config::step::user::data_description::min_cycle_duration%]", | ||
"target_sensor": "[%key:component::generic_hygrostat::config::step::user::data_description::target_sensor%]", | ||
"wet_tolerance": "[%key:component::generic_hygrostat::config::step::user::data_description::wet_tolerance%]" | ||
} | ||
} | ||
} | ||
}, | ||
"selector": { | ||
"device_class": { | ||
"options": { | ||
"humidifier": "Humidifier", | ||
"dehumidifier": "Dehumidifier" | ||
} | ||
} | ||
} | ||
} |
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
66 changes: 66 additions & 0 deletions
66
tests/components/generic_hygrostat/snapshots/test_config_flow.ambr
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,66 @@ | ||
# serializer version: 1 | ||
# name: test_config_flow[create] | ||
FlowResultSnapshot({ | ||
'result': ConfigEntrySnapshot({ | ||
'title': 'My hygrostat', | ||
}), | ||
'title': 'My hygrostat', | ||
'type': <FlowResultType.CREATE_ENTRY: 'create_entry'>, | ||
}) | ||
# --- | ||
# name: test_config_flow[init] | ||
FlowResultSnapshot({ | ||
'type': <FlowResultType.FORM: 'form'>, | ||
}) | ||
# --- | ||
# name: test_options[create_entry] | ||
FlowResultSnapshot({ | ||
'result': True, | ||
'type': <FlowResultType.CREATE_ENTRY: 'create_entry'>, | ||
}) | ||
# --- | ||
# name: test_options[dehumidifier] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'action': <HumidifierAction.OFF: 'off'>, | ||
'current_humidity': 10.0, | ||
'device_class': 'dehumidifier', | ||
'friendly_name': 'My hygrostat', | ||
'humidity': 100, | ||
'max_humidity': 100, | ||
'min_humidity': 0, | ||
'supported_features': <HumidifierEntityFeature: 0>, | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'humidifier.my_hygrostat', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'off', | ||
}) | ||
# --- | ||
# name: test_options[humidifier] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'action': <HumidifierAction.OFF: 'off'>, | ||
'current_humidity': 10.0, | ||
'device_class': 'humidifier', | ||
'friendly_name': 'My hygrostat', | ||
'humidity': 100, | ||
'max_humidity': 100, | ||
'min_humidity': 0, | ||
'supported_features': <HumidifierEntityFeature: 0>, | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'humidifier.my_hygrostat', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'off', | ||
}) | ||
# --- | ||
# name: test_options[init] | ||
FlowResultSnapshot({ | ||
'type': <FlowResultType.FORM: 'form'>, | ||
}) | ||
# --- |
Oops, something went wrong.