Allow ignoring call service events in mqtt_eventstream (#12519)
* [WIP] Allow ignoring call service events This allows a setting a configuration value (False by default to continue the current behavior) which will ignore call service events. * extra spaces removed them * updates from PR review * removed print * update spacing * updated allowed events to allow for custom events, and included some tests * hound fixes * Remove unused constant * Lint
This commit is contained in:
1 parent
4fdbbc497d
commit
f0a1beac5d
2 files changed
+65
-1
No files matched your search
@@ -30,13 +30,16 @@ class TestMqttEventStream(object):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def add_eventstream(self, sub_topic=None, pub_topic=None):
|
||||
def add_eventstream(self, sub_topic=None, pub_topic=None,
|
||||
ignore_event=None):
|
||||
"""Add a mqtt_eventstream component."""
|
||||
config = {}
|
||||
if sub_topic:
|
||||
config['subscribe_topic'] = sub_topic
|
||||
if pub_topic:
|
||||
config['publish_topic'] = pub_topic
|
||||
if ignore_event:
|
||||
config['ignore_event'] = ignore_event
|
||||
return setup_component(self.hass, eventstream.DOMAIN, {
|
||||
eventstream.DOMAIN: config})
|
||||
|
||||
@@ -144,3 +147,57 @@ class TestMqttEventStream(object):
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert 1 == len(calls)
|
||||
|
||||
@patch('homeassistant.components.mqtt.async_publish')
|
||||
def test_ignored_event_doesnt_send_over_stream(self, mock_pub):
|
||||
""""Test the ignoring of sending events if defined."""
|
||||
assert self.add_eventstream(pub_topic='bar',
|
||||
ignore_event=['state_changed'])
|
||||
self.hass.block_till_done()
|
||||
|
||||
e_id = 'entity.test_id'
|
||||
event = {}
|
||||
event['event_type'] = EVENT_STATE_CHANGED
|
||||
new_state = {
|
||||
"state": "on",
|
||||
"entity_id": e_id,
|
||||
"attributes": {},
|
||||
}
|
||||
event['event_data'] = {"new_state": new_state, "entity_id": e_id}
|
||||
|
||||
# Reset the mock because it will have already gotten calls for the
|
||||
# mqtt_eventstream state change on initialization, etc.
|
||||
mock_pub.reset_mock()
|
||||
|
||||
# Set a state of an entity
|
||||
mock_state_change_event(self.hass, State(e_id, 'on'))
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert not mock_pub.called
|
||||
|
||||
@patch('homeassistant.components.mqtt.async_publish')
|
||||
def test_wrong_ignored_event_sends_over_stream(self, mock_pub):
|
||||
""""Test the ignoring of sending events if defined."""
|
||||
assert self.add_eventstream(pub_topic='bar',
|
||||
ignore_event=['statee_changed'])
|
||||
self.hass.block_till_done()
|
||||
|
||||
e_id = 'entity.test_id'
|
||||
event = {}
|
||||
event['event_type'] = EVENT_STATE_CHANGED
|
||||
new_state = {
|
||||
"state": "on",
|
||||
"entity_id": e_id,
|
||||
"attributes": {},
|
||||
}
|
||||
event['event_data'] = {"new_state": new_state, "entity_id": e_id}
|
||||
|
||||
# Reset the mock because it will have already gotten calls for the
|
||||
# mqtt_eventstream state change on initialization, etc.
|
||||
mock_pub.reset_mock()
|
||||
|
||||
# Set a state of an entity
|
||||
mock_state_change_event(self.hass, State(e_id, 'on'))
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert mock_pub.called
|
||||
Reference in new issue
Block a user