core/homeassistant/components/mqtt/schemas.py

200 lines
6.1 KiB
Python
Raw Normal View History

"""Shared schemas for MQTT discovery and YAML config items."""
from __future__ import annotations
Allow MQTT device based auto discovery (#109030) * Add MQTT device based auto discovery * Respect override of component options over shared ones * Add state_topic, command_topic, qos and encoding as shared options * Add shared option test * Rename device.py to schemas.py * Remove unused legacy `platform` attribute to avoid confusion * Split validation device and origin info * Require `origin` info on device based discovery * Log origin info for only once for device discovery * Fix tests and linters * ruff * speed up _replace_all_abbreviations * Fix imports and merging errors - add slots attr * Fix unrelated const changes * More unrelated changes * join string * fix merge * Undo move * Adjust logger statement * fix task storm to load platforms * Revert "fix task storm to load platforms" This reverts commit 8f12a5f2511ab872880a186f5a4605c8bae80c7d. * bail if logging is disabled * Correct mixup object_id and node_id * Auto migrate entities to device discovery * Add device discovery test for device_trigger * Add migration support for non entity platforms * Use helper to remove discovery payload * Fix tests after update branch * Add discovery migration test * Refactor * Repair after rebase * Fix discovery is broken after migration * Improve comments * More comment improvements * Split long lines * Add comment to indicate payload dict can be empty * typo * Add walrus and update comment * Add tag to migration test * Join try blocks * Refactor * Cleanup not used attribute * Refactor * Move _replace_all_abbreviations out of try block --------- Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-29 09:12:05 +00:00
import logging
import voluptuous as vol
from homeassistant.const import (
CONF_DEVICE,
CONF_ENTITY_CATEGORY,
CONF_ICON,
CONF_MODEL,
CONF_NAME,
Allow MQTT device based auto discovery (#109030) * Add MQTT device based auto discovery * Respect override of component options over shared ones * Add state_topic, command_topic, qos and encoding as shared options * Add shared option test * Rename device.py to schemas.py * Remove unused legacy `platform` attribute to avoid confusion * Split validation device and origin info * Require `origin` info on device based discovery * Log origin info for only once for device discovery * Fix tests and linters * ruff * speed up _replace_all_abbreviations * Fix imports and merging errors - add slots attr * Fix unrelated const changes * More unrelated changes * join string * fix merge * Undo move * Adjust logger statement * fix task storm to load platforms * Revert "fix task storm to load platforms" This reverts commit 8f12a5f2511ab872880a186f5a4605c8bae80c7d. * bail if logging is disabled * Correct mixup object_id and node_id * Auto migrate entities to device discovery * Add device discovery test for device_trigger * Add migration support for non entity platforms * Use helper to remove discovery payload * Fix tests after update branch * Add discovery migration test * Refactor * Repair after rebase * Fix discovery is broken after migration * Improve comments * More comment improvements * Split long lines * Add comment to indicate payload dict can be empty * typo * Add walrus and update comment * Add tag to migration test * Join try blocks * Refactor * Cleanup not used attribute * Refactor * Move _replace_all_abbreviations out of try block --------- Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-29 09:12:05 +00:00
CONF_PLATFORM,
CONF_UNIQUE_ID,
CONF_VALUE_TEMPLATE,
)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity import ENTITY_CATEGORIES_SCHEMA
from homeassistant.helpers.typing import ConfigType
from .const import (
AVAILABILITY_LATEST,
AVAILABILITY_MODES,
CONF_AVAILABILITY,
CONF_AVAILABILITY_MODE,
CONF_AVAILABILITY_TEMPLATE,
CONF_AVAILABILITY_TOPIC,
Allow MQTT device based auto discovery (#109030) * Add MQTT device based auto discovery * Respect override of component options over shared ones * Add state_topic, command_topic, qos and encoding as shared options * Add shared option test * Rename device.py to schemas.py * Remove unused legacy `platform` attribute to avoid confusion * Split validation device and origin info * Require `origin` info on device based discovery * Log origin info for only once for device discovery * Fix tests and linters * ruff * speed up _replace_all_abbreviations * Fix imports and merging errors - add slots attr * Fix unrelated const changes * More unrelated changes * join string * fix merge * Undo move * Adjust logger statement * fix task storm to load platforms * Revert "fix task storm to load platforms" This reverts commit 8f12a5f2511ab872880a186f5a4605c8bae80c7d. * bail if logging is disabled * Correct mixup object_id and node_id * Auto migrate entities to device discovery * Add device discovery test for device_trigger * Add migration support for non entity platforms * Use helper to remove discovery payload * Fix tests after update branch * Add discovery migration test * Refactor * Repair after rebase * Fix discovery is broken after migration * Improve comments * More comment improvements * Split long lines * Add comment to indicate payload dict can be empty * typo * Add walrus and update comment * Add tag to migration test * Join try blocks * Refactor * Cleanup not used attribute * Refactor * Move _replace_all_abbreviations out of try block --------- Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-29 09:12:05 +00:00
CONF_COMMAND_TOPIC,
CONF_COMPONENTS,
CONF_CONFIGURATION_URL,
CONF_CONNECTIONS,
CONF_DEPRECATED_VIA_HUB,
CONF_ENABLED_BY_DEFAULT,
Allow MQTT device based auto discovery (#109030) * Add MQTT device based auto discovery * Respect override of component options over shared ones * Add state_topic, command_topic, qos and encoding as shared options * Add shared option test * Rename device.py to schemas.py * Remove unused legacy `platform` attribute to avoid confusion * Split validation device and origin info * Require `origin` info on device based discovery * Log origin info for only once for device discovery * Fix tests and linters * ruff * speed up _replace_all_abbreviations * Fix imports and merging errors - add slots attr * Fix unrelated const changes * More unrelated changes * join string * fix merge * Undo move * Adjust logger statement * fix task storm to load platforms * Revert "fix task storm to load platforms" This reverts commit 8f12a5f2511ab872880a186f5a4605c8bae80c7d. * bail if logging is disabled * Correct mixup object_id and node_id * Auto migrate entities to device discovery * Add device discovery test for device_trigger * Add migration support for non entity platforms * Use helper to remove discovery payload * Fix tests after update branch * Add discovery migration test * Refactor * Repair after rebase * Fix discovery is broken after migration * Improve comments * More comment improvements * Split long lines * Add comment to indicate payload dict can be empty * typo * Add walrus and update comment * Add tag to migration test * Join try blocks * Refactor * Cleanup not used attribute * Refactor * Move _replace_all_abbreviations out of try block --------- Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-29 09:12:05 +00:00
CONF_ENCODING,
CONF_HW_VERSION,
CONF_IDENTIFIERS,
CONF_JSON_ATTRS_TEMPLATE,
CONF_JSON_ATTRS_TOPIC,
CONF_MANUFACTURER,
CONF_OBJECT_ID,
CONF_ORIGIN,
CONF_PAYLOAD_AVAILABLE,
CONF_PAYLOAD_NOT_AVAILABLE,
Allow MQTT device based auto discovery (#109030) * Add MQTT device based auto discovery * Respect override of component options over shared ones * Add state_topic, command_topic, qos and encoding as shared options * Add shared option test * Rename device.py to schemas.py * Remove unused legacy `platform` attribute to avoid confusion * Split validation device and origin info * Require `origin` info on device based discovery * Log origin info for only once for device discovery * Fix tests and linters * ruff * speed up _replace_all_abbreviations * Fix imports and merging errors - add slots attr * Fix unrelated const changes * More unrelated changes * join string * fix merge * Undo move * Adjust logger statement * fix task storm to load platforms * Revert "fix task storm to load platforms" This reverts commit 8f12a5f2511ab872880a186f5a4605c8bae80c7d. * bail if logging is disabled * Correct mixup object_id and node_id * Auto migrate entities to device discovery * Add device discovery test for device_trigger * Add migration support for non entity platforms * Use helper to remove discovery payload * Fix tests after update branch * Add discovery migration test * Refactor * Repair after rebase * Fix discovery is broken after migration * Improve comments * More comment improvements * Split long lines * Add comment to indicate payload dict can be empty * typo * Add walrus and update comment * Add tag to migration test * Join try blocks * Refactor * Cleanup not used attribute * Refactor * Move _replace_all_abbreviations out of try block --------- Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-29 09:12:05 +00:00
CONF_QOS,
CONF_SERIAL_NUMBER,
Allow MQTT device based auto discovery (#109030) * Add MQTT device based auto discovery * Respect override of component options over shared ones * Add state_topic, command_topic, qos and encoding as shared options * Add shared option test * Rename device.py to schemas.py * Remove unused legacy `platform` attribute to avoid confusion * Split validation device and origin info * Require `origin` info on device based discovery * Log origin info for only once for device discovery * Fix tests and linters * ruff * speed up _replace_all_abbreviations * Fix imports and merging errors - add slots attr * Fix unrelated const changes * More unrelated changes * join string * fix merge * Undo move * Adjust logger statement * fix task storm to load platforms * Revert "fix task storm to load platforms" This reverts commit 8f12a5f2511ab872880a186f5a4605c8bae80c7d. * bail if logging is disabled * Correct mixup object_id and node_id * Auto migrate entities to device discovery * Add device discovery test for device_trigger * Add migration support for non entity platforms * Use helper to remove discovery payload * Fix tests after update branch * Add discovery migration test * Refactor * Repair after rebase * Fix discovery is broken after migration * Improve comments * More comment improvements * Split long lines * Add comment to indicate payload dict can be empty * typo * Add walrus and update comment * Add tag to migration test * Join try blocks * Refactor * Cleanup not used attribute * Refactor * Move _replace_all_abbreviations out of try block --------- Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-29 09:12:05 +00:00
CONF_STATE_TOPIC,
CONF_SUGGESTED_AREA,
CONF_SUPPORT_URL,
CONF_SW_VERSION,
CONF_TOPIC,
CONF_VIA_DEVICE,
DEFAULT_PAYLOAD_AVAILABLE,
DEFAULT_PAYLOAD_NOT_AVAILABLE,
Allow MQTT device based auto discovery (#109030) * Add MQTT device based auto discovery * Respect override of component options over shared ones * Add state_topic, command_topic, qos and encoding as shared options * Add shared option test * Rename device.py to schemas.py * Remove unused legacy `platform` attribute to avoid confusion * Split validation device and origin info * Require `origin` info on device based discovery * Log origin info for only once for device discovery * Fix tests and linters * ruff * speed up _replace_all_abbreviations * Fix imports and merging errors - add slots attr * Fix unrelated const changes * More unrelated changes * join string * fix merge * Undo move * Adjust logger statement * fix task storm to load platforms * Revert "fix task storm to load platforms" This reverts commit 8f12a5f2511ab872880a186f5a4605c8bae80c7d. * bail if logging is disabled * Correct mixup object_id and node_id * Auto migrate entities to device discovery * Add device discovery test for device_trigger * Add migration support for non entity platforms * Use helper to remove discovery payload * Fix tests after update branch * Add discovery migration test * Refactor * Repair after rebase * Fix discovery is broken after migration * Improve comments * More comment improvements * Split long lines * Add comment to indicate payload dict can be empty * typo * Add walrus and update comment * Add tag to migration test * Join try blocks * Refactor * Cleanup not used attribute * Refactor * Move _replace_all_abbreviations out of try block --------- Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-29 09:12:05 +00:00
SUPPORTED_COMPONENTS,
)
from .util import valid_publish_topic, valid_qos_schema, valid_subscribe_topic
_LOGGER = logging.getLogger(__name__)
# Device discovery options that are also available at entity component level
SHARED_OPTIONS = [
CONF_AVAILABILITY,
CONF_AVAILABILITY_MODE,
CONF_AVAILABILITY_TEMPLATE,
CONF_AVAILABILITY_TOPIC,
CONF_COMMAND_TOPIC,
CONF_PAYLOAD_AVAILABLE,
CONF_PAYLOAD_NOT_AVAILABLE,
CONF_STATE_TOPIC,
]
MQTT_ORIGIN_INFO_SCHEMA = vol.All(
vol.Schema(
{
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_SW_VERSION): cv.string,
vol.Optional(CONF_SUPPORT_URL): cv.configuration_url,
}
),
)
MQTT_AVAILABILITY_SINGLE_SCHEMA = vol.Schema(
{
vol.Exclusive(CONF_AVAILABILITY_TOPIC, "availability"): valid_subscribe_topic,
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
vol.Optional(
CONF_PAYLOAD_AVAILABLE, default=DEFAULT_PAYLOAD_AVAILABLE
): cv.string,
vol.Optional(
CONF_PAYLOAD_NOT_AVAILABLE, default=DEFAULT_PAYLOAD_NOT_AVAILABLE
): cv.string,
}
)
MQTT_AVAILABILITY_LIST_SCHEMA = vol.Schema(
{
vol.Optional(CONF_AVAILABILITY_MODE, default=AVAILABILITY_LATEST): vol.All(
cv.string, vol.In(AVAILABILITY_MODES)
),
vol.Exclusive(CONF_AVAILABILITY, "availability"): vol.All(
cv.ensure_list,
[
{
vol.Required(CONF_TOPIC): valid_subscribe_topic,
vol.Optional(
CONF_PAYLOAD_AVAILABLE, default=DEFAULT_PAYLOAD_AVAILABLE
): cv.string,
vol.Optional(
CONF_PAYLOAD_NOT_AVAILABLE,
default=DEFAULT_PAYLOAD_NOT_AVAILABLE,
): cv.string,
vol.Optional(CONF_VALUE_TEMPLATE): cv.template,
}
],
),
}
)
MQTT_AVAILABILITY_SCHEMA = MQTT_AVAILABILITY_SINGLE_SCHEMA.extend(
MQTT_AVAILABILITY_LIST_SCHEMA.schema
)
def validate_device_has_at_least_one_identifier(value: ConfigType) -> ConfigType:
"""Validate that a device info entry has at least one identifying value."""
if value.get(CONF_IDENTIFIERS) or value.get(CONF_CONNECTIONS):
return value
raise vol.Invalid(
"Device must have at least one identifying value in "
"'identifiers' and/or 'connections'"
)
MQTT_ENTITY_DEVICE_INFO_SCHEMA = vol.All(
cv.deprecated(CONF_DEPRECATED_VIA_HUB, CONF_VIA_DEVICE),
vol.Schema(
{
vol.Optional(CONF_IDENTIFIERS, default=list): vol.All(
cv.ensure_list, [cv.string]
),
vol.Optional(CONF_CONNECTIONS, default=list): vol.All(
cv.ensure_list, [vol.All(vol.Length(2), [cv.string])]
),
vol.Optional(CONF_MANUFACTURER): cv.string,
vol.Optional(CONF_MODEL): cv.string,
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_HW_VERSION): cv.string,
vol.Optional(CONF_SERIAL_NUMBER): cv.string,
vol.Optional(CONF_SW_VERSION): cv.string,
vol.Optional(CONF_VIA_DEVICE): cv.string,
vol.Optional(CONF_SUGGESTED_AREA): cv.string,
vol.Optional(CONF_CONFIGURATION_URL): cv.configuration_url,
}
),
validate_device_has_at_least_one_identifier,
)
MQTT_ORIGIN_INFO_SCHEMA = vol.All(
vol.Schema(
{
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_SW_VERSION): cv.string,
vol.Optional(CONF_SUPPORT_URL): cv.configuration_url,
}
),
)
MQTT_ENTITY_COMMON_SCHEMA = MQTT_AVAILABILITY_SCHEMA.extend(
{
vol.Optional(CONF_DEVICE): MQTT_ENTITY_DEVICE_INFO_SCHEMA,
vol.Optional(CONF_ORIGIN): MQTT_ORIGIN_INFO_SCHEMA,
vol.Optional(CONF_ENABLED_BY_DEFAULT, default=True): cv.boolean,
vol.Optional(CONF_ENTITY_CATEGORY): ENTITY_CATEGORIES_SCHEMA,
vol.Optional(CONF_ICON): cv.icon,
vol.Optional(CONF_JSON_ATTRS_TOPIC): valid_subscribe_topic,
vol.Optional(CONF_JSON_ATTRS_TEMPLATE): cv.template,
vol.Optional(CONF_OBJECT_ID): cv.string,
vol.Optional(CONF_UNIQUE_ID): cv.string,
}
)
Allow MQTT device based auto discovery (#109030) * Add MQTT device based auto discovery * Respect override of component options over shared ones * Add state_topic, command_topic, qos and encoding as shared options * Add shared option test * Rename device.py to schemas.py * Remove unused legacy `platform` attribute to avoid confusion * Split validation device and origin info * Require `origin` info on device based discovery * Log origin info for only once for device discovery * Fix tests and linters * ruff * speed up _replace_all_abbreviations * Fix imports and merging errors - add slots attr * Fix unrelated const changes * More unrelated changes * join string * fix merge * Undo move * Adjust logger statement * fix task storm to load platforms * Revert "fix task storm to load platforms" This reverts commit 8f12a5f2511ab872880a186f5a4605c8bae80c7d. * bail if logging is disabled * Correct mixup object_id and node_id * Auto migrate entities to device discovery * Add device discovery test for device_trigger * Add migration support for non entity platforms * Use helper to remove discovery payload * Fix tests after update branch * Add discovery migration test * Refactor * Repair after rebase * Fix discovery is broken after migration * Improve comments * More comment improvements * Split long lines * Add comment to indicate payload dict can be empty * typo * Add walrus and update comment * Add tag to migration test * Join try blocks * Refactor * Cleanup not used attribute * Refactor * Move _replace_all_abbreviations out of try block --------- Co-authored-by: J. Nick Koston <nick@koston.org>
2024-05-29 09:12:05 +00:00
COMPONENT_CONFIG_SCHEMA = vol.Schema(
{vol.Required(CONF_PLATFORM): vol.In(SUPPORTED_COMPONENTS)}
).extend({}, extra=True)
DEVICE_DISCOVERY_SCHEMA = MQTT_AVAILABILITY_SCHEMA.extend(
{
vol.Required(CONF_DEVICE): MQTT_ENTITY_DEVICE_INFO_SCHEMA,
vol.Required(CONF_COMPONENTS): vol.Schema({str: COMPONENT_CONFIG_SCHEMA}),
vol.Required(CONF_ORIGIN): MQTT_ORIGIN_INFO_SCHEMA,
vol.Optional(CONF_STATE_TOPIC): valid_subscribe_topic,
vol.Optional(CONF_COMMAND_TOPIC): valid_publish_topic,
vol.Optional(CONF_QOS): valid_qos_schema,
vol.Optional(CONF_ENCODING): cv.string,
}
)