2021-01-06 08:27:46 +01:00
|
|
|
"""Configure number in a device through MQTT topic."""
|
2021-10-25 09:44:43 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-01-09 14:37:33 +01:00
|
|
|
import functools
|
2021-01-06 08:27:46 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components import number
|
2021-05-21 17:35:27 +02:00
|
|
|
from homeassistant.components.number import (
|
|
|
|
DEFAULT_MAX_VALUE,
|
|
|
|
DEFAULT_MIN_VALUE,
|
|
|
|
DEFAULT_STEP,
|
2022-06-16 13:34:54 +02:00
|
|
|
DEVICE_CLASSES_SCHEMA,
|
2022-07-02 19:15:54 +02:00
|
|
|
NumberDeviceClass,
|
2022-06-16 13:34:54 +02:00
|
|
|
RestoreNumber,
|
2021-05-21 17:35:27 +02:00
|
|
|
)
|
2022-01-03 08:53:52 +01:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-10-25 09:44:43 +02:00
|
|
|
from homeassistant.const import (
|
2022-06-16 13:34:54 +02:00
|
|
|
CONF_DEVICE_CLASS,
|
2021-10-25 09:44:43 +02:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_OPTIMISTIC,
|
|
|
|
CONF_UNIT_OF_MEASUREMENT,
|
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
)
|
2021-04-22 23:53:37 +02:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-01-06 08:27:46 +01:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2022-01-03 08:53:52 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2021-01-06 08:27:46 +01:00
|
|
|
|
2022-05-31 09:32:44 +02:00
|
|
|
from . import subscription
|
|
|
|
from .config import MQTT_RW_SCHEMA
|
2022-01-03 09:03:47 +01:00
|
|
|
from .const import (
|
2022-01-20 16:15:26 +01:00
|
|
|
CONF_COMMAND_TEMPLATE,
|
2022-01-03 09:03:47 +01:00
|
|
|
CONF_COMMAND_TOPIC,
|
|
|
|
CONF_ENCODING,
|
|
|
|
CONF_QOS,
|
|
|
|
CONF_RETAIN,
|
|
|
|
CONF_STATE_TOPIC,
|
|
|
|
)
|
2021-01-06 08:27:46 +01:00
|
|
|
from .debug_info import log_messages
|
2022-02-10 21:09:57 +01:00
|
|
|
from .mixins import (
|
|
|
|
MQTT_ENTITY_COMMON_SCHEMA,
|
|
|
|
MqttEntity,
|
2022-06-30 19:15:25 +02:00
|
|
|
async_discover_yaml_entities,
|
2022-02-10 21:09:57 +01:00
|
|
|
async_setup_entry_helper,
|
|
|
|
async_setup_platform_helper,
|
2022-05-23 08:03:09 +02:00
|
|
|
warn_for_legacy_schema,
|
2022-02-10 21:09:57 +01:00
|
|
|
)
|
2022-05-31 09:32:44 +02:00
|
|
|
from .models import MqttCommandTemplate, MqttValueTemplate
|
2021-01-06 08:27:46 +01:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-05-21 17:35:27 +02:00
|
|
|
CONF_MIN = "min"
|
|
|
|
CONF_MAX = "max"
|
2021-10-07 13:54:34 +02:00
|
|
|
CONF_PAYLOAD_RESET = "payload_reset"
|
2021-05-21 17:35:27 +02:00
|
|
|
CONF_STEP = "step"
|
|
|
|
|
2021-01-06 08:27:46 +01:00
|
|
|
DEFAULT_NAME = "MQTT Number"
|
2021-01-08 23:43:14 +01:00
|
|
|
DEFAULT_OPTIMISTIC = False
|
2021-10-07 13:54:34 +02:00
|
|
|
DEFAULT_PAYLOAD_RESET = "None"
|
2021-01-06 08:27:46 +01:00
|
|
|
|
2021-06-29 11:16:57 +02:00
|
|
|
MQTT_NUMBER_ATTRIBUTES_BLOCKED = frozenset(
|
|
|
|
{
|
|
|
|
number.ATTR_MAX,
|
|
|
|
number.ATTR_MIN,
|
|
|
|
number.ATTR_STEP,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-05-21 17:35:27 +02:00
|
|
|
|
|
|
|
def validate_config(config):
|
|
|
|
"""Validate that the configuration is valid, throws if it isn't."""
|
|
|
|
if config.get(CONF_MIN) >= config.get(CONF_MAX):
|
2021-06-23 15:35:18 +02:00
|
|
|
raise vol.Invalid(f"'{CONF_MAX}' must be > '{CONF_MIN}'")
|
2021-05-21 17:35:27 +02:00
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
|
2022-05-31 09:32:44 +02:00
|
|
|
_PLATFORM_SCHEMA_BASE = MQTT_RW_SCHEMA.extend(
|
2021-10-25 13:47:06 +02:00
|
|
|
{
|
2021-11-02 18:21:49 +01:00
|
|
|
vol.Optional(CONF_COMMAND_TEMPLATE): cv.template,
|
2022-06-16 13:34:54 +02:00
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
2021-10-25 13:47:06 +02:00
|
|
|
vol.Optional(CONF_MAX, default=DEFAULT_MAX_VALUE): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_MIN, default=DEFAULT_MIN_VALUE): vol.Coerce(float),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
|
|
|
|
vol.Optional(CONF_PAYLOAD_RESET, default=DEFAULT_PAYLOAD_RESET): cv.string,
|
|
|
|
vol.Optional(CONF_STEP, default=DEFAULT_STEP): vol.All(
|
|
|
|
vol.Coerce(float), vol.Range(min=1e-3)
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
|
|
|
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
},
|
|
|
|
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
|
|
|
|
|
2022-05-23 08:03:09 +02:00
|
|
|
PLATFORM_SCHEMA_MODERN = vol.All(
|
2021-10-25 13:47:06 +02:00
|
|
|
_PLATFORM_SCHEMA_BASE,
|
|
|
|
validate_config,
|
|
|
|
)
|
|
|
|
|
2022-05-23 08:03:09 +02:00
|
|
|
# Configuring MQTT Number under the number platform key is deprecated in HA Core 2022.6
|
|
|
|
PLATFORM_SCHEMA = vol.All(
|
|
|
|
cv.PLATFORM_SCHEMA.extend(_PLATFORM_SCHEMA_BASE.schema),
|
|
|
|
validate_config,
|
|
|
|
warn_for_legacy_schema(number.DOMAIN),
|
|
|
|
)
|
|
|
|
|
2021-10-25 13:47:06 +02:00
|
|
|
DISCOVERY_SCHEMA = vol.All(
|
|
|
|
_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA),
|
2021-05-21 17:35:27 +02:00
|
|
|
validate_config,
|
|
|
|
)
|
2021-01-06 08:27:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(
|
2022-01-03 08:53:52 +01:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2022-05-23 08:03:09 +02:00
|
|
|
"""Set up MQTT number configured under the number platform key (deprecated)."""
|
|
|
|
# Deprecated in HA Core 2022.6
|
2022-02-10 21:09:57 +01:00
|
|
|
await async_setup_platform_helper(
|
2022-06-02 20:32:31 +02:00
|
|
|
hass,
|
|
|
|
number.DOMAIN,
|
|
|
|
discovery_info or config,
|
|
|
|
async_add_entities,
|
|
|
|
_async_setup_entity,
|
2022-02-10 21:09:57 +01:00
|
|
|
)
|
2021-01-06 08:27:46 +01:00
|
|
|
|
|
|
|
|
2022-01-03 08:53:52 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2022-05-23 08:03:09 +02:00
|
|
|
"""Set up MQTT number through configuration.yaml and dynamically through MQTT discovery."""
|
|
|
|
# load and initialize platform config from configuration.yaml
|
2022-06-30 19:15:25 +02:00
|
|
|
await async_discover_yaml_entities(hass, number.DOMAIN)
|
2022-05-23 08:03:09 +02:00
|
|
|
# setup for discovery
|
2021-01-09 14:37:33 +01:00
|
|
|
setup = functools.partial(
|
2021-06-28 10:23:34 +02:00
|
|
|
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
|
2021-01-06 08:27:46 +01:00
|
|
|
)
|
2021-10-25 13:47:06 +02:00
|
|
|
await async_setup_entry_helper(hass, number.DOMAIN, setup, DISCOVERY_SCHEMA)
|
2021-01-06 08:27:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def _async_setup_entity(
|
2022-07-12 11:07:18 +02:00
|
|
|
hass: HomeAssistant,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
config: ConfigType,
|
|
|
|
config_entry: ConfigEntry | None = None,
|
|
|
|
discovery_data: dict | None = None,
|
|
|
|
) -> None:
|
2021-01-06 08:27:46 +01:00
|
|
|
"""Set up the MQTT number."""
|
2021-06-28 10:23:34 +02:00
|
|
|
async_add_entities([MqttNumber(hass, config, config_entry, discovery_data)])
|
2021-01-06 08:27:46 +01:00
|
|
|
|
|
|
|
|
2022-06-16 13:34:54 +02:00
|
|
|
class MqttNumber(MqttEntity, RestoreNumber):
|
2021-01-06 08:27:46 +01:00
|
|
|
"""representation of an MQTT number."""
|
|
|
|
|
2021-11-08 14:02:18 +01:00
|
|
|
_entity_id_format = number.ENTITY_ID_FORMAT
|
2021-06-29 11:16:57 +02:00
|
|
|
_attributes_extra_blocked = MQTT_NUMBER_ATTRIBUTES_BLOCKED
|
|
|
|
|
2021-06-28 10:23:34 +02:00
|
|
|
def __init__(self, hass, config, config_entry, discovery_data):
|
2021-01-06 08:27:46 +01:00
|
|
|
"""Initialize the MQTT Number."""
|
2021-05-21 17:35:27 +02:00
|
|
|
self._config = config
|
2021-06-23 15:35:18 +02:00
|
|
|
self._optimistic = False
|
2021-01-06 08:27:46 +01:00
|
|
|
self._sub_state = None
|
|
|
|
|
|
|
|
self._current_number = None
|
|
|
|
|
2022-06-16 13:34:54 +02:00
|
|
|
RestoreNumber.__init__(self)
|
2021-06-28 10:23:34 +02:00
|
|
|
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
|
2021-01-09 17:46:53 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def config_schema():
|
|
|
|
"""Return the config schema."""
|
2021-10-25 13:47:06 +02:00
|
|
|
return DISCOVERY_SCHEMA
|
2021-01-09 17:46:53 +01:00
|
|
|
|
2021-06-23 15:35:18 +02:00
|
|
|
def _setup_from_config(self, config):
|
|
|
|
"""(Re)Setup the entity."""
|
|
|
|
self._optimistic = config[CONF_OPTIMISTIC]
|
|
|
|
|
2021-11-02 18:21:49 +01:00
|
|
|
self._templates = {
|
2021-12-15 11:28:43 +01:00
|
|
|
CONF_COMMAND_TEMPLATE: MqttCommandTemplate(
|
2022-01-03 15:10:15 +01:00
|
|
|
config.get(CONF_COMMAND_TEMPLATE), entity=self
|
2021-12-15 11:28:43 +01:00
|
|
|
).async_render,
|
2022-01-03 16:07:40 +01:00
|
|
|
CONF_VALUE_TEMPLATE: MqttValueTemplate(
|
|
|
|
config.get(CONF_VALUE_TEMPLATE),
|
|
|
|
entity=self,
|
|
|
|
).async_render_with_possible_json_value,
|
2021-11-02 18:21:49 +01:00
|
|
|
}
|
2021-12-15 11:28:43 +01:00
|
|
|
|
2022-02-03 02:12:22 +01:00
|
|
|
def _prepare_subscribe_topics(self):
|
2021-01-06 08:27:46 +01:00
|
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
@log_messages(self.hass, self.entity_id)
|
|
|
|
def message_received(msg):
|
|
|
|
"""Handle new MQTT messages."""
|
2021-11-02 18:21:49 +01:00
|
|
|
payload = self._templates[CONF_VALUE_TEMPLATE](msg.payload)
|
2021-01-06 08:27:46 +01:00
|
|
|
try:
|
2021-10-07 13:54:34 +02:00
|
|
|
if payload == self._config[CONF_PAYLOAD_RESET]:
|
|
|
|
num_value = None
|
|
|
|
elif payload.isnumeric():
|
2021-06-28 10:23:34 +02:00
|
|
|
num_value = int(payload)
|
2021-01-06 08:27:46 +01:00
|
|
|
else:
|
2021-06-28 10:23:34 +02:00
|
|
|
num_value = float(payload)
|
2021-01-06 08:27:46 +01:00
|
|
|
except ValueError:
|
2021-06-23 15:35:18 +02:00
|
|
|
_LOGGER.warning("Payload '%s' is not a Number", msg.payload)
|
2021-05-21 17:35:27 +02:00
|
|
|
return
|
|
|
|
|
2021-10-07 13:54:34 +02:00
|
|
|
if num_value is not None and (
|
|
|
|
num_value < self.min_value or num_value > self.max_value
|
|
|
|
):
|
2021-05-21 17:35:27 +02:00
|
|
|
_LOGGER.error(
|
|
|
|
"Invalid value for %s: %s (range %s - %s)",
|
|
|
|
self.entity_id,
|
|
|
|
num_value,
|
|
|
|
self.min_value,
|
|
|
|
self.max_value,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
self._current_number = num_value
|
|
|
|
self.async_write_ha_state()
|
2021-01-06 08:27:46 +01:00
|
|
|
|
2021-01-08 23:43:14 +01:00
|
|
|
if self._config.get(CONF_STATE_TOPIC) is None:
|
|
|
|
# Force into optimistic mode.
|
|
|
|
self._optimistic = True
|
|
|
|
else:
|
2022-02-03 02:12:22 +01:00
|
|
|
self._sub_state = subscription.async_prepare_subscribe_topics(
|
2021-01-08 23:43:14 +01:00
|
|
|
self.hass,
|
|
|
|
self._sub_state,
|
|
|
|
{
|
|
|
|
"state_topic": {
|
|
|
|
"topic": self._config.get(CONF_STATE_TOPIC),
|
|
|
|
"msg_callback": message_received,
|
|
|
|
"qos": self._config[CONF_QOS],
|
2022-01-03 16:08:07 +01:00
|
|
|
"encoding": self._config[CONF_ENCODING] or None,
|
2021-01-08 23:43:14 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-02-03 02:12:22 +01:00
|
|
|
async def _subscribe_topics(self):
|
|
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
|
|
|
|
2022-06-16 13:34:54 +02:00
|
|
|
if self._optimistic and (
|
|
|
|
last_number_data := await self.async_get_last_number_data()
|
|
|
|
):
|
|
|
|
self._current_number = last_number_data.native_value
|
2021-01-06 08:27:46 +01:00
|
|
|
|
2021-05-21 17:35:27 +02:00
|
|
|
@property
|
2022-06-16 13:34:54 +02:00
|
|
|
def native_min_value(self) -> float:
|
2021-05-21 17:35:27 +02:00
|
|
|
"""Return the minimum value."""
|
|
|
|
return self._config[CONF_MIN]
|
|
|
|
|
|
|
|
@property
|
2022-06-16 13:34:54 +02:00
|
|
|
def native_max_value(self) -> float:
|
2021-05-21 17:35:27 +02:00
|
|
|
"""Return the maximum value."""
|
|
|
|
return self._config[CONF_MAX]
|
|
|
|
|
|
|
|
@property
|
2022-06-16 13:34:54 +02:00
|
|
|
def native_step(self) -> float:
|
2021-05-21 17:35:27 +02:00
|
|
|
"""Return the increment/decrement step."""
|
|
|
|
return self._config[CONF_STEP]
|
|
|
|
|
2021-10-25 09:44:43 +02:00
|
|
|
@property
|
2022-06-16 13:34:54 +02:00
|
|
|
def native_unit_of_measurement(self) -> str | None:
|
2021-10-25 09:44:43 +02:00
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self._config.get(CONF_UNIT_OF_MEASUREMENT)
|
|
|
|
|
2021-01-06 08:27:46 +01:00
|
|
|
@property
|
2022-08-11 00:10:28 +02:00
|
|
|
def native_value(self) -> float | None:
|
2021-01-06 08:27:46 +01:00
|
|
|
"""Return the current value."""
|
|
|
|
return self._current_number
|
|
|
|
|
2022-06-16 13:34:54 +02:00
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
2021-01-06 08:27:46 +01:00
|
|
|
"""Update the current value."""
|
2021-01-08 23:43:14 +01:00
|
|
|
current_number = value
|
|
|
|
|
2021-01-06 08:27:46 +01:00
|
|
|
if value.is_integer():
|
2021-01-08 23:43:14 +01:00
|
|
|
current_number = int(value)
|
2021-11-02 18:21:49 +01:00
|
|
|
payload = self._templates[CONF_COMMAND_TEMPLATE](current_number)
|
2021-01-08 23:43:14 +01:00
|
|
|
|
|
|
|
if self._optimistic:
|
|
|
|
self._current_number = current_number
|
|
|
|
self.async_write_ha_state()
|
2021-01-06 08:27:46 +01:00
|
|
|
|
2022-02-04 17:35:32 +01:00
|
|
|
await self.async_publish(
|
2021-01-08 23:43:14 +01:00
|
|
|
self._config[CONF_COMMAND_TOPIC],
|
2021-11-02 18:21:49 +01:00
|
|
|
payload,
|
2021-01-06 08:27:46 +01:00
|
|
|
self._config[CONF_QOS],
|
2021-03-02 14:28:31 +01:00
|
|
|
self._config[CONF_RETAIN],
|
2022-01-03 09:03:47 +01:00
|
|
|
self._config[CONF_ENCODING],
|
2021-01-06 08:27:46 +01:00
|
|
|
)
|
|
|
|
|
2021-01-08 23:43:14 +01:00
|
|
|
@property
|
2022-07-02 19:15:54 +02:00
|
|
|
def assumed_state(self) -> bool:
|
2021-01-08 23:43:14 +01:00
|
|
|
"""Return true if we do optimistic updates."""
|
|
|
|
return self._optimistic
|
2022-06-16 13:34:54 +02:00
|
|
|
|
|
|
|
@property
|
2022-07-02 19:15:54 +02:00
|
|
|
def device_class(self) -> NumberDeviceClass | None:
|
2022-06-16 13:34:54 +02:00
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return self._config.get(CONF_DEVICE_CLASS)
|