Add QoS parameter to MQTT websocket_subscribe (#83241)

This commit is contained in:
Jan Bouwhuis 2022-12-06 09:31:19 +01:00 committed by GitHub
parent 3de7b439b8
commit 7ed1d4988b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View file

@ -573,6 +573,7 @@ def websocket_mqtt_info(
{ {
vol.Required("type"): "mqtt/subscribe", vol.Required("type"): "mqtt/subscribe",
vol.Required("topic"): valid_subscribe_topic, vol.Required("topic"): valid_subscribe_topic,
vol.Optional("qos"): valid_qos_schema,
} }
) )
@websocket_api.async_response @websocket_api.async_response
@ -606,8 +607,9 @@ async def websocket_subscribe(
) )
# Perform UTF-8 decoding directly in callback routine # Perform UTF-8 decoding directly in callback routine
qos: int = msg["qos"] if "qos" in msg else DEFAULT_QOS
connection.subscriptions[msg["id"]] = await async_subscribe( connection.subscriptions[msg["id"]] = await async_subscribe(
hass, msg["topic"], forward_messages, encoding=None hass, msg["topic"], forward_messages, encoding=None, qos=qos
) )
connection.send_message(websocket_api.result_message(msg["id"])) connection.send_message(websocket_api.result_message(msg["id"]))

View file

@ -2021,6 +2021,37 @@ async def test_mqtt_ws_subscription(
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
# Subscribe with QoS 2
await client.send_json(
{"id": 9, "type": "mqtt/subscribe", "topic": "test-topic", "qos": 2}
)
response = await client.receive_json()
assert response["success"]
async_fire_mqtt_message(hass, "test-topic", "test1", 2)
async_fire_mqtt_message(hass, "test-topic", "test2", 2)
async_fire_mqtt_message(hass, "test-topic", b"\xDE\xAD\xBE\xEF", 2)
response = await client.receive_json()
assert response["event"]["topic"] == "test-topic"
assert response["event"]["payload"] == "test1"
assert response["event"]["qos"] == 2
response = await client.receive_json()
assert response["event"]["topic"] == "test-topic"
assert response["event"]["payload"] == "test2"
assert response["event"]["qos"] == 2
response = await client.receive_json()
assert response["event"]["topic"] == "test-topic"
assert response["event"]["payload"] == "b'\\xde\\xad\\xbe\\xef'"
assert response["event"]["qos"] == 2
# Unsubscribe
await client.send_json({"id": 15, "type": "unsubscribe_events", "subscription": 9})
response = await client.receive_json()
assert response["success"]
async def test_mqtt_ws_subscription_not_admin( async def test_mqtt_ws_subscription_not_admin(
hass, hass_ws_client, mqtt_mock_entry_no_yaml_config, hass_read_only_access_token hass, hass_ws_client, mqtt_mock_entry_no_yaml_config, hass_read_only_access_token