1 """Support for numbers which integrates with other components."""
3 from __future__
import annotations
8 import voluptuous
as vol
15 DOMAIN
as NUMBER_DOMAIN,
25 CONF_UNIT_OF_MEASUREMENT,
34 from .
import TriggerUpdateCoordinator
35 from .const
import CONF_MAX, CONF_MIN, CONF_STEP, DOMAIN
36 from .template_entity
import (
37 TEMPLATE_ENTITY_AVAILABILITY_SCHEMA,
38 TEMPLATE_ENTITY_ICON_SCHEMA,
41 from .trigger_entity
import TriggerEntity
43 _LOGGER = logging.getLogger(__name__)
45 CONF_SET_VALUE =
"set_value"
47 DEFAULT_NAME =
"Template Number"
48 DEFAULT_OPTIMISTIC =
False
53 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template,
54 vol.Required(CONF_STATE): cv.template,
55 vol.Required(CONF_SET_VALUE): cv.SCRIPT_SCHEMA,
56 vol.Required(CONF_STEP): cv.template,
57 vol.Optional(CONF_MIN, default=DEFAULT_MIN_VALUE): cv.template,
58 vol.Optional(CONF_MAX, default=DEFAULT_MAX_VALUE): cv.template,
59 vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
60 vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
61 vol.Optional(CONF_UNIQUE_ID): cv.string,
64 .extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA.schema)
65 .extend(TEMPLATE_ENTITY_ICON_SCHEMA.schema)
67 NUMBER_CONFIG_SCHEMA = vol.Schema(
69 vol.Required(CONF_NAME): cv.template,
70 vol.Required(CONF_STATE): cv.template,
71 vol.Required(CONF_STEP): cv.template,
72 vol.Required(CONF_SET_VALUE): cv.SCRIPT_SCHEMA,
73 vol.Optional(CONF_MIN): cv.template,
74 vol.Optional(CONF_MAX): cv.template,
75 vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
76 vol.Optional(CONF_DEVICE_ID): selector.DeviceSelector(),
82 hass: HomeAssistant, definitions: list[dict[str, Any]], unique_id_prefix: str |
None
83 ) -> list[TemplateNumber]:
84 """Create the Template number."""
86 for definition
in definitions:
87 unique_id = definition.get(CONF_UNIQUE_ID)
88 if unique_id
and unique_id_prefix:
89 unique_id = f
"{unique_id_prefix}-{unique_id}"
97 async_add_entities: AddEntitiesCallback,
98 discovery_info: DiscoveryInfoType |
None =
None,
100 """Set up the template number."""
101 if discovery_info
is None:
103 "Template number entities can only be configured under template:"
107 if "coordinator" in discovery_info:
110 for config
in discovery_info[
"entities"]
116 hass, discovery_info[
"entities"], discovery_info[
"unique_id"]
123 config_entry: ConfigEntry,
124 async_add_entities: AddEntitiesCallback,
126 """Initialize config entry."""
127 _options =
dict(config_entry.options)
128 _options.pop(
"template_type")
135 hass: HomeAssistant, name: str, config: dict[str, Any]
137 """Create a preview number."""
143 """Representation of a template number."""
145 _attr_should_poll =
False
151 unique_id: str |
None,
153 """Initialize the number."""
154 super().
__init__(hass, config=config, unique_id=unique_id)
158 hass, config[CONF_SET_VALUE], self.
_attr_name_attr_name, DOMAIN
171 config.get(CONF_DEVICE_ID),
176 """Set up templates."""
178 "_attr_native_value",
180 validator=vol.Coerce(float),
181 none_on_template_error=
True,
186 validator=vol.Coerce(float),
187 none_on_template_error=
True,
191 "_attr_native_min_value",
193 validator=vol.Coerce(float),
194 none_on_template_error=
True,
198 "_attr_native_max_value",
200 validator=vol.Coerce(float),
201 none_on_template_error=
True,
206 """Set value of the number."""
213 run_variables={ATTR_VALUE: value},
219 """Number entity based on trigger data."""
221 domain = NUMBER_DOMAIN
222 extra_template_keys = (
232 coordinator: TriggerUpdateCoordinator,
235 """Initialize the entity."""
236 super().
__init__(hass, coordinator, config)
240 config[CONF_SET_VALUE],
241 self._rendered.
get(CONF_NAME, DEFAULT_NAME),
249 """Return the currently selected option."""
250 return vol.Any(vol.Coerce(float),
None)(self._rendered.
get(CONF_STATE))
254 """Return the minimum value."""
255 return vol.Any(vol.Coerce(float),
None)(
256 self._rendered.
get(CONF_MIN, super().native_min_value)
261 """Return the maximum value."""
262 return vol.Any(vol.Coerce(float),
None)(
263 self._rendered.
get(CONF_MAX, super().native_max_value)
268 """Return the increment/decrement step."""
269 return vol.Any(vol.Coerce(float),
None)(
270 self._rendered.
get(CONF_STEP, super().native_step)
274 """Set value of the number."""
275 if self._config[CONF_OPTIMISTIC]:
279 {ATTR_VALUE: value}, context=self.
_context_context
None __init__(self, HomeAssistant hass, config, str|None unique_id)
None _async_setup_templates(self)
None async_set_native_value(self, float value)
_attr_native_unit_of_measurement
_attr_native_unit_of_measurement
int native_min_value(self)
int native_max_value(self)
None __init__(self, HomeAssistant hass, TriggerUpdateCoordinator coordinator, dict config)
float|None native_value(self)
None async_set_native_value(self, float value)
None async_run_script(self, Script script, *_VarsType|None run_variables=None, Context|None context=None)
None add_template_attribute(self, str attribute, Template template, Callable[[Any], Any]|None validator=None, Callable[[Any], None]|None on_update=None, bool none_on_template_error=False)
None async_write_ha_state(self)
web.Response get(self, web.Request request, str config_key)
list[TemplateNumber] _async_create_entities(HomeAssistant hass, list[dict[str, Any]] definitions, str|None unique_id_prefix)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
TemplateNumber async_create_preview_number(HomeAssistant hass, str name, dict[str, Any] config)
dr.DeviceInfo|None async_device_info_to_link_from_device_id(HomeAssistant hass, str|None device_id)
def async_run(config_dir)