mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Minor refactor of template number (#61863)
This commit is contained in:
parent
c9320b5ca1
commit
bb3a3bbc1b
@ -17,22 +17,20 @@ from homeassistant.components.number.const import (
|
|||||||
DEFAULT_MIN_VALUE,
|
DEFAULT_MIN_VALUE,
|
||||||
DOMAIN as NUMBER_DOMAIN,
|
DOMAIN as NUMBER_DOMAIN,
|
||||||
)
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import CONF_NAME, CONF_OPTIMISTIC, CONF_STATE, CONF_UNIQUE_ID
|
||||||
CONF_ICON,
|
|
||||||
CONF_NAME,
|
|
||||||
CONF_OPTIMISTIC,
|
|
||||||
CONF_STATE,
|
|
||||||
CONF_UNIQUE_ID,
|
|
||||||
)
|
|
||||||
from homeassistant.core import Config, HomeAssistant
|
from homeassistant.core import Config, HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.script import Script
|
from homeassistant.helpers.script import Script
|
||||||
from homeassistant.helpers.template import Template, TemplateError
|
from homeassistant.helpers.template import TemplateError
|
||||||
|
|
||||||
from . import TriggerUpdateCoordinator
|
from . import TriggerUpdateCoordinator
|
||||||
from .const import CONF_AVAILABILITY, DOMAIN
|
from .const import DOMAIN
|
||||||
from .template_entity import TemplateEntity
|
from .template_entity import (
|
||||||
|
TEMPLATE_ENTITY_AVAILABILITY_SCHEMA,
|
||||||
|
TEMPLATE_ENTITY_ICON_SCHEMA,
|
||||||
|
TemplateEntity,
|
||||||
|
)
|
||||||
from .trigger_entity import TriggerEntity
|
from .trigger_entity import TriggerEntity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -42,7 +40,8 @@ CONF_SET_VALUE = "set_value"
|
|||||||
DEFAULT_NAME = "Template Number"
|
DEFAULT_NAME = "Template Number"
|
||||||
DEFAULT_OPTIMISTIC = False
|
DEFAULT_OPTIMISTIC = False
|
||||||
|
|
||||||
NUMBER_SCHEMA = vol.Schema(
|
NUMBER_SCHEMA = (
|
||||||
|
vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.template,
|
||||||
vol.Required(CONF_STATE): cv.template,
|
vol.Required(CONF_STATE): cv.template,
|
||||||
@ -50,11 +49,12 @@ NUMBER_SCHEMA = vol.Schema(
|
|||||||
vol.Required(ATTR_STEP): cv.template,
|
vol.Required(ATTR_STEP): cv.template,
|
||||||
vol.Optional(ATTR_MIN, default=DEFAULT_MIN_VALUE): cv.template,
|
vol.Optional(ATTR_MIN, default=DEFAULT_MIN_VALUE): cv.template,
|
||||||
vol.Optional(ATTR_MAX, default=DEFAULT_MAX_VALUE): cv.template,
|
vol.Optional(ATTR_MAX, default=DEFAULT_MAX_VALUE): cv.template,
|
||||||
vol.Optional(CONF_AVAILABILITY): cv.template,
|
|
||||||
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
||||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||||
vol.Optional(CONF_ICON): cv.template,
|
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
.extend(TEMPLATE_ENTITY_AVAILABILITY_SCHEMA.schema)
|
||||||
|
.extend(TEMPLATE_ENTITY_ICON_SCHEMA.schema)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -67,21 +67,7 @@ async def _async_create_entities(
|
|||||||
unique_id = definition.get(CONF_UNIQUE_ID)
|
unique_id = definition.get(CONF_UNIQUE_ID)
|
||||||
if unique_id and unique_id_prefix:
|
if unique_id and unique_id_prefix:
|
||||||
unique_id = f"{unique_id_prefix}-{unique_id}"
|
unique_id = f"{unique_id_prefix}-{unique_id}"
|
||||||
entities.append(
|
entities.append(TemplateNumber(hass, definition, unique_id))
|
||||||
TemplateNumber(
|
|
||||||
hass,
|
|
||||||
definition[CONF_NAME],
|
|
||||||
definition[CONF_STATE],
|
|
||||||
definition.get(CONF_AVAILABILITY),
|
|
||||||
definition[CONF_SET_VALUE],
|
|
||||||
definition[ATTR_STEP],
|
|
||||||
definition[ATTR_MIN],
|
|
||||||
definition[ATTR_MAX],
|
|
||||||
definition[CONF_OPTIMISTIC],
|
|
||||||
unique_id,
|
|
||||||
definition.get(CONF_ICON),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return entities
|
return entities
|
||||||
|
|
||||||
|
|
||||||
@ -118,34 +104,24 @@ class TemplateNumber(TemplateEntity, NumberEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
name_template: Template,
|
config,
|
||||||
value_template: Template,
|
|
||||||
availability_template: Template | None,
|
|
||||||
command_set_value: dict[str, Any],
|
|
||||||
step_template: Template,
|
|
||||||
minimum_template: Template | None,
|
|
||||||
maximum_template: Template | None,
|
|
||||||
optimistic: bool,
|
|
||||||
unique_id: str | None,
|
unique_id: str | None,
|
||||||
icon_template: Template | None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the number."""
|
"""Initialize the number."""
|
||||||
super().__init__(
|
super().__init__(config=config)
|
||||||
availability_template=availability_template, icon_template=icon_template
|
|
||||||
)
|
|
||||||
self._attr_name = DEFAULT_NAME
|
self._attr_name = DEFAULT_NAME
|
||||||
self._name_template = name_template
|
self._name_template = name_template = config[CONF_NAME]
|
||||||
name_template.hass = hass
|
name_template.hass = hass
|
||||||
with contextlib.suppress(TemplateError):
|
with contextlib.suppress(TemplateError):
|
||||||
self._attr_name = name_template.async_render(parse_result=False)
|
self._attr_name = name_template.async_render(parse_result=False)
|
||||||
self._value_template = value_template
|
self._value_template = config[CONF_STATE]
|
||||||
self._command_set_value = Script(
|
self._command_set_value = Script(
|
||||||
hass, command_set_value, self._attr_name, DOMAIN
|
hass, config[CONF_SET_VALUE], self._attr_name, DOMAIN
|
||||||
)
|
)
|
||||||
self._step_template = step_template
|
self._step_template = config[ATTR_STEP]
|
||||||
self._min_value_template = minimum_template
|
self._min_value_template = config[ATTR_MIN]
|
||||||
self._max_value_template = maximum_template
|
self._max_value_template = config[ATTR_MAX]
|
||||||
self._attr_assumed_state = self._optimistic = optimistic
|
self._attr_assumed_state = self._optimistic = config[CONF_OPTIMISTIC]
|
||||||
self._attr_unique_id = unique_id
|
self._attr_unique_id = unique_id
|
||||||
self._attr_value = None
|
self._attr_value = None
|
||||||
self._attr_step = None
|
self._attr_step = None
|
||||||
|
@ -37,6 +37,18 @@ from .const import (
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
TEMPLATE_ENTITY_AVAILABILITY_SCHEMA = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Optional(CONF_AVAILABILITY): cv.template,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
TEMPLATE_ENTITY_ICON_SCHEMA = vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Optional(CONF_ICON): cv.template,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
TEMPLATE_ENTITY_COMMON_SCHEMA = vol.Schema(
|
TEMPLATE_ENTITY_COMMON_SCHEMA = vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_ATTRIBUTES): vol.Schema({cv.string: cv.template}),
|
vol.Optional(CONF_ATTRIBUTES): vol.Schema({cv.string: cv.template}),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user