From f58106674729692c66c109f11da58870d4fb2f10 Mon Sep 17 00:00:00 2001 From: dougiteixeira <31328123+dougiteixeira@users.noreply.github.com> Date: Thu, 15 Aug 2024 15:02:51 -0300 Subject: [PATCH] Add config flow for platform number in Template (#121849) * Add config flow to select platform in Template * Remove device id duplicate in schema * Add config flow for number platform in Template * Remove mode --- .../components/template/config_flow.py | 37 +++++++ homeassistant/components/template/number.py | 97 ++++++++++++++----- .../components/template/strings.json | 31 ++++++ .../template/snapshots/test_number.ambr | 18 ++++ tests/components/template/test_config_flow.py | 64 ++++++++++++ tests/components/template/test_init.py | 16 +++ tests/components/template/test_number.py | 78 ++++++++++++++- 7 files changed, 314 insertions(+), 27 deletions(-) create mode 100644 tests/components/template/snapshots/test_number.ambr diff --git a/homeassistant/components/template/config_flow.py b/homeassistant/components/template/config_flow.py index c52a890c1f7..2c12a0d03e9 100644 --- a/homeassistant/components/template/config_flow.py +++ b/homeassistant/components/template/config_flow.py @@ -41,6 +41,16 @@ from homeassistant.helpers.schema_config_entry_flow import ( from .binary_sensor import async_create_preview_binary_sensor from .const import CONF_PRESS, CONF_TURN_OFF, CONF_TURN_ON, DOMAIN +from .number import ( + CONF_MAX, + CONF_MIN, + CONF_SET_VALUE, + CONF_STEP, + DEFAULT_MAX_VALUE, + DEFAULT_MIN_VALUE, + DEFAULT_STEP, + async_create_preview_number, +) from .select import CONF_OPTIONS, CONF_SELECT_OPTION from .sensor import async_create_preview_sensor from .switch import async_create_preview_switch @@ -94,6 +104,21 @@ def generate_schema(domain: str, flow_type: str) -> vol.Schema: vol.Optional(CONF_VERIFY_SSL, default=True): selector.BooleanSelector(), } + if domain == Platform.NUMBER: + schema |= { + vol.Required(CONF_STATE): selector.TemplateSelector(), + vol.Required( + CONF_MIN, default=f"{{{{{DEFAULT_MIN_VALUE}}}}}" + ): selector.TemplateSelector(), + vol.Required( + CONF_MAX, default=f"{{{{{DEFAULT_MAX_VALUE}}}}}" + ): selector.TemplateSelector(), + vol.Required( + CONF_STEP, default=f"{{{{{DEFAULT_STEP}}}}}" + ): selector.TemplateSelector(), + vol.Optional(CONF_SET_VALUE): selector.ActionSelector(), + } + if domain == Platform.SELECT: schema |= _SCHEMA_STATE | { vol.Required(CONF_OPTIONS): selector.TemplateSelector(), @@ -238,6 +263,7 @@ TEMPLATE_TYPES = [ "binary_sensor", "button", "image", + "number", "select", "sensor", "switch", @@ -258,6 +284,11 @@ CONFIG_FLOW = { config_schema(Platform.IMAGE), validate_user_input=validate_user_input(Platform.IMAGE), ), + Platform.NUMBER: SchemaFlowFormStep( + config_schema(Platform.NUMBER), + preview="template", + validate_user_input=validate_user_input(Platform.NUMBER), + ), Platform.SELECT: SchemaFlowFormStep( config_schema(Platform.SELECT), validate_user_input=validate_user_input(Platform.SELECT), @@ -290,6 +321,11 @@ OPTIONS_FLOW = { options_schema(Platform.IMAGE), validate_user_input=validate_user_input(Platform.IMAGE), ), + Platform.NUMBER: SchemaFlowFormStep( + options_schema(Platform.NUMBER), + preview="template", + validate_user_input=validate_user_input(Platform.NUMBER), + ), Platform.SELECT: SchemaFlowFormStep( options_schema(Platform.SELECT), validate_user_input=validate_user_input(Platform.SELECT), @@ -311,6 +347,7 @@ CREATE_PREVIEW_ENTITY: dict[ Callable[[HomeAssistant, str, dict[str, Any]], TemplateEntity], ] = { "binary_sensor": async_create_preview_binary_sensor, + "number": async_create_preview_number, "sensor": async_create_preview_sensor, "switch": async_create_preview_switch, } diff --git a/homeassistant/components/template/number.py b/homeassistant/components/template/number.py index d4004ee9535..955600a9b9e 100644 --- a/homeassistant/components/template/number.py +++ b/homeassistant/components/template/number.py @@ -8,9 +8,6 @@ from typing import Any import voluptuous as vol from homeassistant.components.number import ( - ATTR_MAX, - ATTR_MIN, - ATTR_STEP, ATTR_VALUE, DEFAULT_MAX_VALUE, DEFAULT_MIN_VALUE, @@ -18,9 +15,17 @@ from homeassistant.components.number import ( DOMAIN as NUMBER_DOMAIN, NumberEntity, ) -from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_STATE, CONF_UNIQUE_ID +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import ( + CONF_DEVICE_ID, + CONF_NAME, + CONF_OPTIMISTIC, + CONF_STATE, + CONF_UNIQUE_ID, +) from homeassistant.core import HomeAssistant, callback -import homeassistant.helpers.config_validation as cv +from homeassistant.helpers import config_validation as cv, selector +from homeassistant.helpers.device import async_device_info_to_link_from_device_id from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.script import Script from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType @@ -37,6 +42,9 @@ from .trigger_entity import TriggerEntity _LOGGER = logging.getLogger(__name__) CONF_SET_VALUE = "set_value" +CONF_MIN = "min" +CONF_MAX = "max" +CONF_STEP = "step" DEFAULT_NAME = "Template Number" DEFAULT_OPTIMISTIC = False @@ -47,9 +55,9 @@ NUMBER_SCHEMA = ( vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template, vol.Required(CONF_STATE): cv.template, vol.Required(CONF_SET_VALUE): cv.SCRIPT_SCHEMA, - vol.Required(ATTR_STEP): cv.template, - vol.Optional(ATTR_MIN, default=DEFAULT_MIN_VALUE): cv.template, - vol.Optional(ATTR_MAX, default=DEFAULT_MAX_VALUE): cv.template, + vol.Required(CONF_STEP): cv.template, + vol.Optional(CONF_MIN, default=DEFAULT_MIN_VALUE): cv.template, + vol.Optional(CONF_MAX, default=DEFAULT_MAX_VALUE): cv.template, vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean, vol.Optional(CONF_UNIQUE_ID): cv.string, } @@ -57,6 +65,17 @@ NUMBER_SCHEMA = ( .extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA.schema) .extend(TEMPLATE_ENTITY_ICON_SCHEMA.schema) ) +NUMBER_CONFIG_SCHEMA = vol.Schema( + { + vol.Required(CONF_NAME): cv.template, + vol.Required(CONF_STATE): cv.template, + vol.Required(CONF_STEP): cv.template, + vol.Optional(CONF_SET_VALUE): cv.SCRIPT_SCHEMA, + vol.Optional(CONF_MIN): cv.template, + vol.Optional(CONF_MAX): cv.template, + vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(), + } +) async def _async_create_entities( @@ -99,6 +118,27 @@ async def async_setup_platform( ) +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Initialize config entry.""" + _options = dict(config_entry.options) + _options.pop("template_type") + validated_config = NUMBER_CONFIG_SCHEMA(_options) + async_add_entities([TemplateNumber(hass, validated_config, config_entry.entry_id)]) + + +@callback +def async_create_preview_number( + hass: HomeAssistant, name: str, config: dict[str, Any] +) -> TemplateNumber: + """Create a preview number.""" + validated_config = NUMBER_CONFIG_SCHEMA(config | {CONF_NAME: name}) + return TemplateNumber(hass, validated_config, None) + + class TemplateNumber(TemplateEntity, NumberEntity): """Representation of a template number.""" @@ -114,16 +154,22 @@ class TemplateNumber(TemplateEntity, NumberEntity): super().__init__(hass, config=config, unique_id=unique_id) assert self._attr_name is not None self._value_template = config[CONF_STATE] - self._command_set_value = Script( - hass, config[CONF_SET_VALUE], self._attr_name, DOMAIN + self._command_set_value = ( + Script(hass, config[CONF_SET_VALUE], self._attr_name, DOMAIN) + if config.get(CONF_SET_VALUE, None) is not None + else None ) - self._step_template = config[ATTR_STEP] - self._min_value_template = config[ATTR_MIN] - self._max_value_template = config[ATTR_MAX] - self._attr_assumed_state = self._optimistic = config[CONF_OPTIMISTIC] + self._step_template = config[CONF_STEP] + self._min_value_template = config[CONF_MIN] + self._max_value_template = config[CONF_MAX] + self._attr_assumed_state = self._optimistic = config.get(CONF_OPTIMISTIC) self._attr_native_step = DEFAULT_STEP self._attr_native_min_value = DEFAULT_MIN_VALUE self._attr_native_max_value = DEFAULT_MAX_VALUE + self._attr_device_info = async_device_info_to_link_from_device_id( + hass, + config.get(CONF_DEVICE_ID), + ) @callback def _async_setup_templates(self) -> None: @@ -161,11 +207,12 @@ class TemplateNumber(TemplateEntity, NumberEntity): if self._optimistic: self._attr_native_value = value self.async_write_ha_state() - await self.async_run_script( - self._command_set_value, - run_variables={ATTR_VALUE: value}, - context=self._context, - ) + if self._command_set_value: + await self.async_run_script( + self._command_set_value, + run_variables={ATTR_VALUE: value}, + context=self._context, + ) class TriggerNumberEntity(TriggerEntity, NumberEntity): @@ -174,9 +221,9 @@ class TriggerNumberEntity(TriggerEntity, NumberEntity): domain = NUMBER_DOMAIN extra_template_keys = ( CONF_STATE, - ATTR_STEP, - ATTR_MIN, - ATTR_MAX, + CONF_STEP, + CONF_MIN, + CONF_MAX, ) def __init__( @@ -203,21 +250,21 @@ class TriggerNumberEntity(TriggerEntity, NumberEntity): def native_min_value(self) -> int: """Return the minimum value.""" return vol.Any(vol.Coerce(float), None)( - self._rendered.get(ATTR_MIN, super().native_min_value) + self._rendered.get(CONF_MIN, super().native_min_value) ) @property def native_max_value(self) -> int: """Return the maximum value.""" return vol.Any(vol.Coerce(float), None)( - self._rendered.get(ATTR_MAX, super().native_max_value) + self._rendered.get(CONF_MAX, super().native_max_value) ) @property def native_step(self) -> int: """Return the increment/decrement step.""" return vol.Any(vol.Coerce(float), None)( - self._rendered.get(ATTR_STEP, super().native_step) + self._rendered.get(CONF_STEP, super().native_step) ) async def async_set_native_value(self, value: float) -> None: diff --git a/homeassistant/components/template/strings.json b/homeassistant/components/template/strings.json index b1f14af2202..fa365bf3cfd 100644 --- a/homeassistant/components/template/strings.json +++ b/homeassistant/components/template/strings.json @@ -37,6 +37,21 @@ }, "title": "Template image" }, + "number": { + "data": { + "device_id": "[%key:common::config_flow::data::device%]", + "name": "[%key:common::config_flow::data::name%]", + "state": "[%key:component::template::config::step::sensor::data::state%]", + "step": "Step value", + "set_value": "Actions on set value", + "max": "Maximum value", + "min": "Minimum value" + }, + "data_description": { + "device_id": "[%key:component::template::config::step::sensor::data_description::device_id%]" + }, + "title": "Template number" + }, "select": { "data": { "device_id": "[%key:common::config_flow::data::device%]", @@ -70,6 +85,7 @@ "binary_sensor": "Template a binary sensor", "button": "Template a button", "image": "Template a image", + "number": "Template a number", "select": "Template a select", "sensor": "Template a sensor", "switch": "Template a switch" @@ -125,6 +141,21 @@ }, "title": "[%key:component::template::config::step::image::title%]" }, + "number": { + "data": { + "device_id": "[%key:common::config_flow::data::device%]", + "name": "[%key:common::config_flow::data::name%]", + "state": "[%key:component::template::config::step::sensor::data::state%]", + "step": "[%key:component::template::config::step::number::data::step%]", + "set_value": "[%key:component::template::config::step::number::data::set_value%]", + "max": "[%key:component::template::config::step::number::data::max%]", + "min": "[%key:component::template::config::step::number::data::min%]" + }, + "data_description": { + "device_id": "[%key:component::template::config::step::sensor::data_description::device_id%]" + }, + "title": "[%key:component::template::config::step::number::title%]" + }, "select": { "data": { "device_id": "[%key:common::config_flow::data::device%]", diff --git a/tests/components/template/snapshots/test_number.ambr b/tests/components/template/snapshots/test_number.ambr new file mode 100644 index 00000000000..d6f5b1e338d --- /dev/null +++ b/tests/components/template/snapshots/test_number.ambr @@ -0,0 +1,18 @@ +# serializer version: 1 +# name: test_setup_config_entry + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'My template', + 'max': 100.0, + 'min': 0.0, + 'mode': , + 'step': 0.1, + }), + 'context': , + 'entity_id': 'number.my_template', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '10.0', + }) +# --- \ No newline at end of file diff --git a/tests/components/template/test_config_flow.py b/tests/components/template/test_config_flow.py index ff5db52d667..a62370f4261 100644 --- a/tests/components/template/test_config_flow.py +++ b/tests/components/template/test_config_flow.py @@ -91,6 +91,24 @@ from tests.typing import WebSocketGenerator {"verify_ssl": True}, {}, ), + ( + "number", + {"state": "{{ states('number.one') }}"}, + "30.0", + {"one": "30.0", "two": "20.0"}, + {}, + { + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + { + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + {}, + ), ( "select", {"state": "{{ states('select.one') }}"}, @@ -226,6 +244,20 @@ async def test_config_flow( {"verify_ssl": True}, {"verify_ssl": True}, ), + ( + "number", + {"state": "{{ states('number.one') }}"}, + { + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + { + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + ), ( "select", {"state": "{{ states('select.one') }}"}, @@ -402,6 +434,24 @@ def get_suggested(schema, key): }, "url", ), + ( + "number", + {"state": "{{ states('number.one') }}"}, + {"state": "{{ states('number.two') }}"}, + ["30.0", "20.0"], + {"one": "30.0", "two": "20.0"}, + { + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + { + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + "state", + ), ( "select", {"state": "{{ states('select.one') }}"}, @@ -1156,6 +1206,20 @@ async def test_option_flow_sensor_preview_config_entry_removed( {}, {}, ), + ( + "number", + {"state": "{{ states('number.one') }}"}, + { + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + { + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + ), ( "select", {"state": "{{ states('select.one') }}"}, diff --git a/tests/components/template/test_init.py b/tests/components/template/test_init.py index fe08e1f4963..2e5870217a2 100644 --- a/tests/components/template/test_init.py +++ b/tests/components/template/test_init.py @@ -314,6 +314,22 @@ async def async_yaml_patch_helper(hass, filename): }, {}, ), + ( + { + "template_type": "number", + "name": "My template", + "state": "{{ 10 }}", + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + { + "state": "{{ 11 }}", + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + ), ( { "template_type": "select", diff --git a/tests/components/template/test_number.py b/tests/components/template/test_number.py index bf04151fd36..ca9fe2d7688 100644 --- a/tests/components/template/test_number.py +++ b/tests/components/template/test_number.py @@ -1,5 +1,7 @@ """The tests for the Template number platform.""" +from syrupy.assertion import SnapshotAssertion + from homeassistant import setup from homeassistant.components.input_number import ( ATTR_VALUE as INPUT_NUMBER_ATTR_VALUE, @@ -14,11 +16,12 @@ from homeassistant.components.number import ( DOMAIN as NUMBER_DOMAIN, SERVICE_SET_VALUE as NUMBER_SERVICE_SET_VALUE, ) +from homeassistant.components.template import DOMAIN from homeassistant.const import ATTR_ICON, CONF_ENTITY_ID, STATE_UNKNOWN from homeassistant.core import Context, HomeAssistant, ServiceCall -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import device_registry as dr, entity_registry as er -from tests.common import assert_setup_component, async_capture_events +from tests.common import MockConfigEntry, assert_setup_component, async_capture_events _TEST_NUMBER = "number.template_number" # Represent for number's value @@ -42,6 +45,35 @@ _VALUE_INPUT_NUMBER_CONFIG = { } +async def test_setup_config_entry( + hass: HomeAssistant, + snapshot: SnapshotAssertion, +) -> None: + """Test the config flow.""" + + template_config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={ + "name": "My template", + "template_type": "number", + "state": "{{ 10 }}", + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + }, + title="My template", + ) + template_config_entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(template_config_entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get("number.my_template") + assert state is not None + assert state == snapshot + + async def test_missing_optional_config(hass: HomeAssistant) -> None: """Test: missing optional template is ok.""" with assert_setup_component(1, "template"): @@ -460,3 +492,45 @@ async def test_icon_template_with_trigger(hass: HomeAssistant) -> None: state = hass.states.get(_TEST_NUMBER) assert float(state.state) == 51 assert state.attributes[ATTR_ICON] == "mdi:greater" + + +async def test_device_id( + hass: HomeAssistant, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test for device for number template.""" + + device_config_entry = MockConfigEntry() + device_config_entry.add_to_hass(hass) + device_entry = device_registry.async_get_or_create( + config_entry_id=device_config_entry.entry_id, + identifiers={("test", "identifier_test")}, + connections={("mac", "30:31:32:33:34:35")}, + ) + await hass.async_block_till_done() + assert device_entry is not None + assert device_entry.id is not None + + template_config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={ + "name": "My template", + "template_type": "number", + "state": "{{ 10 }}", + "min": "{{ 0 }}", + "max": "{{ 100 }}", + "step": "{{ 0.1 }}", + "device_id": device_entry.id, + }, + title="My template", + ) + template_config_entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(template_config_entry.entry_id) + await hass.async_block_till_done() + + template_entity = entity_registry.async_get("number.my_template") + assert template_entity is not None + assert template_entity.device_id == device_entry.id