Don't receive homeassistant_* events from MQTT eventstream (#49158)
parent
769923e8dd
commit
916ba0be11
|
@ -7,6 +7,11 @@ from homeassistant.components.mqtt import valid_publish_topic, valid_subscribe_t
|
|||
from homeassistant.const import (
|
||||
ATTR_SERVICE_DATA,
|
||||
EVENT_CALL_SERVICE,
|
||||
EVENT_HOMEASSISTANT_CLOSE,
|
||||
EVENT_HOMEASSISTANT_FINAL_WRITE,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STARTED,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
EVENT_STATE_CHANGED,
|
||||
EVENT_TIME_CHANGED,
|
||||
MATCH_ALL,
|
||||
|
@ -37,6 +42,14 @@ CONFIG_SCHEMA = vol.Schema(
|
|||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
BLOCKED_EVENTS = [
|
||||
EVENT_HOMEASSISTANT_CLOSE,
|
||||
EVENT_HOMEASSISTANT_START,
|
||||
EVENT_HOMEASSISTANT_STARTED,
|
||||
EVENT_HOMEASSISTANT_STOP,
|
||||
EVENT_HOMEASSISTANT_FINAL_WRITE,
|
||||
]
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up the MQTT eventstream component."""
|
||||
|
@ -45,16 +58,15 @@ async def async_setup(hass, config):
|
|||
pub_topic = conf.get(CONF_PUBLISH_TOPIC)
|
||||
sub_topic = conf.get(CONF_SUBSCRIBE_TOPIC)
|
||||
ignore_event = conf.get(CONF_IGNORE_EVENT)
|
||||
ignore_event.append(EVENT_TIME_CHANGED)
|
||||
|
||||
@callback
|
||||
def _event_publisher(event):
|
||||
"""Handle events by publishing them on the MQTT queue."""
|
||||
if event.origin != EventOrigin.local:
|
||||
return
|
||||
if event.event_type == EVENT_TIME_CHANGED:
|
||||
return
|
||||
|
||||
# User-defined events to ignore
|
||||
# Events to ignore
|
||||
if event.event_type in ignore_event:
|
||||
return
|
||||
|
||||
|
@ -84,6 +96,10 @@ async def async_setup(hass, config):
|
|||
event_type = event.get("event_type")
|
||||
event_data = event.get("event_data")
|
||||
|
||||
# Don't fire HOMEASSISTANT_* events on this instance
|
||||
if event_type in BLOCKED_EVENTS:
|
||||
return
|
||||
|
||||
# Special case handling for event STATE_CHANGED
|
||||
# We will try to convert state dicts back to State objects
|
||||
# Copied over from the _handle_api_post_events_event method
|
||||
|
|
|
@ -3,7 +3,7 @@ import json
|
|||
from unittest.mock import ANY, patch
|
||||
|
||||
import homeassistant.components.mqtt_eventstream as eventstream
|
||||
from homeassistant.const import EVENT_STATE_CHANGED
|
||||
from homeassistant.const import EVENT_STATE_CHANGED, MATCH_ALL
|
||||
from homeassistant.core import State, callback
|
||||
from homeassistant.helpers.json import JSONEncoder
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -114,6 +114,7 @@ async def test_time_event_does_not_send_message(hass, mqtt_mock):
|
|||
mqtt_mock.async_publish.reset_mock()
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow())
|
||||
await hass.async_block_till_done()
|
||||
assert not mqtt_mock.async_publish.called
|
||||
|
||||
|
||||
|
@ -140,6 +141,33 @@ async def test_receiving_remote_event_fires_hass_event(hass, mqtt_mock):
|
|||
|
||||
assert len(calls) == 1
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_receiving_blocked_event_fires_hass_event(hass, mqtt_mock):
|
||||
"""Test the receiving of blocked event does not fire."""
|
||||
sub_topic = "foo"
|
||||
assert await add_eventstream(hass, sub_topic=sub_topic)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
calls = []
|
||||
|
||||
@callback
|
||||
def listener(_):
|
||||
calls.append(1)
|
||||
|
||||
hass.bus.async_listen(MATCH_ALL, listener)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
for event in eventstream.BLOCKED_EVENTS:
|
||||
payload = json.dumps({"event_type": event, "event_data": {}}, cls=JSONEncoder)
|
||||
async_fire_mqtt_message(hass, sub_topic, payload)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(calls) == 0
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
async def test_ignored_event_doesnt_send_over_stream(hass, mqtt_mock):
|
||||
"""Test the ignoring of sending events if defined."""
|
||||
|
@ -159,6 +187,7 @@ async def test_ignored_event_doesnt_send_over_stream(hass, mqtt_mock):
|
|||
# Set a state of an entity
|
||||
mock_state_change_event(hass, State(e_id, "on"))
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not mqtt_mock.async_publish.called
|
||||
|
||||
|
|
Loading…
Reference in New Issue