Add WS subscription command for MQTT (#21696)

* Add WS subscription command for MQTT

* Add test

* Add check for connected

* Rename event_listeners to subscriptions
This commit is contained in:
Paulus Schoutsen 2019-03-10 20:07:09 -07:00 committed by GitHub
parent fc85b3fc5f
commit 429bbc05dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 90 additions and 17 deletions

View file

@ -24,15 +24,6 @@ def async_register_commands(hass):
async_reg(handle_ping)
def event_message(iden, event):
"""Return an event message."""
return {
'id': iden,
'type': 'event',
'event': event.as_dict(),
}
def pong_message(iden):
"""Return a pong message."""
return {
@ -59,9 +50,11 @@ def handle_subscribe_events(hass, connection, msg):
if event.event_type == EVENT_TIME_CHANGED:
return
connection.send_message(event_message(msg['id'], event))
connection.send_message(messages.event_message(
msg['id'], event.as_dict()
))
connection.event_listeners[msg['id']] = hass.bus.async_listen(
connection.subscriptions[msg['id']] = hass.bus.async_listen(
msg['event_type'], forward_events)
connection.send_message(messages.result_message(msg['id']))
@ -79,8 +72,8 @@ def handle_unsubscribe_events(hass, connection, msg):
"""
subscription = msg['subscription']
if subscription in connection.event_listeners:
connection.event_listeners.pop(subscription)()
if subscription in connection.subscriptions:
connection.subscriptions.pop(subscription)()
connection.send_message(messages.result_message(msg['id']))
else:
connection.send_message(messages.error_message(