mirror of https://github.com/home-assistant/core
92 lines
3.1 KiB
Python
92 lines
3.1 KiB
Python
"""Support for MQTT buttons."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components import button
|
|
from homeassistant.components.button import DEVICE_CLASSES_SCHEMA, ButtonEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_DEVICE_CLASS, CONF_NAME
|
|
from homeassistant.core import HomeAssistant, callback
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
from .config import DEFAULT_RETAIN, MQTT_BASE_SCHEMA
|
|
from .const import CONF_COMMAND_TEMPLATE, CONF_COMMAND_TOPIC, CONF_RETAIN
|
|
from .entity import MqttEntity, async_setup_entity_entry_helper
|
|
from .models import MqttCommandTemplate
|
|
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
|
from .util import valid_publish_topic
|
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
CONF_PAYLOAD_PRESS = "payload_press"
|
|
DEFAULT_NAME = "MQTT Button"
|
|
DEFAULT_PAYLOAD_PRESS = "PRESS"
|
|
|
|
PLATFORM_SCHEMA_MODERN = MQTT_BASE_SCHEMA.extend(
|
|
{
|
|
vol.Optional(CONF_COMMAND_TEMPLATE): cv.template,
|
|
vol.Required(CONF_COMMAND_TOPIC): valid_publish_topic,
|
|
vol.Optional(CONF_DEVICE_CLASS): vol.Any(DEVICE_CLASSES_SCHEMA, None),
|
|
vol.Optional(CONF_NAME): vol.Any(cv.string, None),
|
|
vol.Optional(CONF_PAYLOAD_PRESS, default=DEFAULT_PAYLOAD_PRESS): cv.string,
|
|
vol.Optional(CONF_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
|
|
}
|
|
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
|
|
|
|
DISCOVERY_SCHEMA = PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.REMOVE_EXTRA)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up MQTT button through YAML and through MQTT discovery."""
|
|
async_setup_entity_entry_helper(
|
|
hass,
|
|
config_entry,
|
|
MqttButton,
|
|
button.DOMAIN,
|
|
async_add_entities,
|
|
DISCOVERY_SCHEMA,
|
|
PLATFORM_SCHEMA_MODERN,
|
|
)
|
|
|
|
|
|
class MqttButton(MqttEntity, ButtonEntity):
|
|
"""Representation of a switch that can be toggled using MQTT."""
|
|
|
|
_default_name = DEFAULT_NAME
|
|
_entity_id_format = button.ENTITY_ID_FORMAT
|
|
|
|
@staticmethod
|
|
def config_schema() -> vol.Schema:
|
|
"""Return the config schema."""
|
|
return DISCOVERY_SCHEMA
|
|
|
|
def _setup_from_config(self, config: ConfigType) -> None:
|
|
"""(Re)Setup the entity."""
|
|
self._command_template = MqttCommandTemplate(
|
|
config.get(CONF_COMMAND_TEMPLATE), entity=self
|
|
).async_render
|
|
self._attr_device_class = self._config.get(CONF_DEVICE_CLASS)
|
|
|
|
@callback
|
|
def _prepare_subscribe_topics(self) -> None:
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
async def _subscribe_topics(self) -> None:
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
async def async_press(self) -> None:
|
|
"""Turn the device on.
|
|
|
|
This method is a coroutine.
|
|
"""
|
|
payload = self._command_template(self._config[CONF_PAYLOAD_PRESS])
|
|
await self.async_publish_with_config(self._config[CONF_COMMAND_TOPIC], payload)
|