1 """Support for MQTT events."""
3 from __future__
import annotations
5 from collections.abc
import Callable
9 import voluptuous
as vol
26 from .
import subscription
27 from .config
import MQTT_RO_SCHEMA
28 from .const
import CONF_STATE_TOPIC, PAYLOAD_EMPTY_JSON, PAYLOAD_NONE
29 from .entity
import MqttEntity, async_setup_entity_entry_helper
33 MqttValueTemplateException,
37 from .schemas
import MQTT_ENTITY_COMMON_SCHEMA
39 _LOGGER = logging.getLogger(__name__)
43 CONF_EVENT_TYPES =
"event_types"
45 MQTT_EVENT_ATTRIBUTES_BLOCKED = frozenset(
47 event.ATTR_EVENT_TYPE,
48 event.ATTR_EVENT_TYPES,
52 DEFAULT_NAME =
"MQTT Event"
53 DEFAULT_FORCE_UPDATE =
False
54 DEVICE_CLASS_SCHEMA = vol.All(vol.Lower, vol.Coerce(EventDeviceClass))
56 _PLATFORM_SCHEMA_BASE = MQTT_RO_SCHEMA.extend(
58 vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASS_SCHEMA,
59 vol.Optional(CONF_NAME): vol.Any(
None, cv.string),
60 vol.Required(CONF_EVENT_TYPES): vol.All(cv.ensure_list, [cv.string]),
62 ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
64 PLATFORM_SCHEMA_MODERN = vol.All(
65 _PLATFORM_SCHEMA_BASE,
68 DISCOVERY_SCHEMA = vol.All(
69 _PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA),
75 config_entry: ConfigEntry,
76 async_add_entities: AddEntitiesCallback,
78 """Set up MQTT event through YAML and through MQTT discovery."""
86 PLATFORM_SCHEMA_MODERN,
91 """Representation of an event that can be updated using MQTT."""
93 _default_name = DEFAULT_NAME
94 _entity_id_format = ENTITY_ID_FORMAT
95 _attributes_extra_blocked = MQTT_EVENT_ATTRIBUTES_BLOCKED
96 _template: Callable[[ReceivePayloadType, PayloadSentinel], ReceivePayloadType]
100 """Return the config schema."""
101 return DISCOVERY_SCHEMA
104 """(Re)Setup the entity."""
108 self.
_config_config.
get(CONF_VALUE_TEMPLATE), entity=self
109 ).async_render_with_possible_json_value
113 """Handle new MQTT messages."""
116 "Ignoring event trigger from replayed retained payload '%s' on topic %s",
121 event_attributes: dict[str, Any] = {}
124 payload = self.
_template_template(msg.payload, PayloadSentinel.DEFAULT)
125 except MqttValueTemplateException
as exc:
130 or payload
is PayloadSentinel.DEFAULT
131 or payload
in (PAYLOAD_NONE, PAYLOAD_EMPTY_JSON)
134 "Ignoring empty payload '%s' after rendering for topic %s",
141 event_type =
str(event_attributes.pop(event.ATTR_EVENT_TYPE))
144 "JSON event data detected after processing payload '%s' on"
145 " topic %s, type %s, attributes %s"
154 (
"`event_type` missing in JSON event payload, " " '%s' on topic %s"),
159 except JSON_DECODE_EXCEPTIONS:
162 "No valid JSON event payload detected, "
163 "value after processing payload"
174 "Invalid event type %s for %s received on topic %s, payload %s",
181 mqtt_data = self.
hasshasshass.data[DATA_MQTT]
182 mqtt_data.state_write_requests.write_state_request(self)
186 """(Re)Subscribe to topics."""
190 """(Re)Subscribe to topics."""
191 subscription.async_subscribe_topics_internal(self.
hasshasshass, self.
_sub_state_sub_state)
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
bool add_subscription(self, str state_topic_config_key, Callable[[ReceiveMessage], None] msg_callback, set[str]|None tracked_attributes, bool disable_encoding=False)
None _event_received(self, ReceiveMessage msg)
None _prepare_subscribe_topics(self)
None _setup_from_config(self, ConfigType config)
None _subscribe_topics(self)
VolSchemaType config_schema()
web.Response get(self, web.Request request, str config_key)
None async_setup_entity_entry_helper(HomeAssistant hass, ConfigEntry entry, type[MqttEntity]|None entity_class, str domain, AddEntitiesCallback async_add_entities, VolSchemaType discovery_schema, VolSchemaType platform_schema_modern, dict[str, type[MqttEntity]]|None schema_class_mapping=None)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
JsonObjectType json_loads_object(bytes|bytearray|memoryview|str obj)