mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Improve code coverage MQTT integration (#64546)
This commit is contained in:
parent
f9cd67675f
commit
31a9d64572
@ -2338,7 +2338,7 @@ async def test_custom_availability_payload(hass, mqtt_mock):
|
|||||||
|
|
||||||
|
|
||||||
async def test_valid_device_class(hass, mqtt_mock):
|
async def test_valid_device_class(hass, mqtt_mock):
|
||||||
"""Test the setting of a valid sensor class."""
|
"""Test the setting of a valid device class."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
cover.DOMAIN,
|
cover.DOMAIN,
|
||||||
@ -2358,7 +2358,7 @@ async def test_valid_device_class(hass, mqtt_mock):
|
|||||||
|
|
||||||
|
|
||||||
async def test_invalid_device_class(hass, mqtt_mock):
|
async def test_invalid_device_class(hass, mqtt_mock):
|
||||||
"""Test the setting of an invalid sensor class."""
|
"""Test the setting of an invalid device class."""
|
||||||
assert await async_setup_component(
|
assert await async_setup_component(
|
||||||
hass,
|
hass,
|
||||||
cover.DOMAIN,
|
cover.DOMAIN,
|
||||||
|
@ -21,6 +21,7 @@ import homeassistant.core as ha
|
|||||||
from homeassistant.core import CoreState, callback
|
from homeassistant.core import CoreState, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import device_registry as dr, template
|
from homeassistant.helpers import device_registry as dr, template
|
||||||
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
from homeassistant.util.dt import utcnow
|
from homeassistant.util.dt import utcnow
|
||||||
|
|
||||||
@ -244,18 +245,18 @@ async def test_value_template_value(hass):
|
|||||||
variables = {"id": 1234, "some_var": "beer"}
|
variables = {"id": 1234, "some_var": "beer"}
|
||||||
|
|
||||||
# test rendering value
|
# test rendering value
|
||||||
tpl = template.Template("{{ value_json.id }}", hass)
|
tpl = template.Template("{{ value_json.id }}")
|
||||||
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass)
|
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass)
|
||||||
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
|
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
|
||||||
|
|
||||||
# test variables at rendering
|
# test variables at rendering
|
||||||
tpl = template.Template("{{ value_json.id }} {{ some_var }}", hass)
|
tpl = template.Template("{{ value_json.id }} {{ some_var }} {{ code }}")
|
||||||
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass)
|
val_tpl = mqtt.MqttValueTemplate(tpl, hass=hass, config_attributes={"code": 1234})
|
||||||
assert (
|
assert (
|
||||||
val_tpl.async_render_with_possible_json_value(
|
val_tpl.async_render_with_possible_json_value(
|
||||||
'{"id": 4321}', variables=variables
|
'{"id": 4321}', variables=variables
|
||||||
)
|
)
|
||||||
== "4321 beer"
|
== "4321 beer 1234"
|
||||||
)
|
)
|
||||||
|
|
||||||
# test with default value if an error occurs due to an invalid template
|
# test with default value if an error occurs due to an invalid template
|
||||||
@ -266,6 +267,13 @@ async def test_value_template_value(hass):
|
|||||||
== "my default"
|
== "my default"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# test value template with entity
|
||||||
|
entity = Entity()
|
||||||
|
entity.hass = hass
|
||||||
|
tpl = template.Template("{{ value_json.id }}")
|
||||||
|
val_tpl = mqtt.MqttValueTemplate(tpl, entity=entity)
|
||||||
|
assert val_tpl.async_render_with_possible_json_value('{"id": 4321}') == "4321"
|
||||||
|
|
||||||
|
|
||||||
async def test_service_call_without_topic_does_not_publish(hass, mqtt_mock):
|
async def test_service_call_without_topic_does_not_publish(hass, mqtt_mock):
|
||||||
"""Test the service call if topic is missing."""
|
"""Test the service call if topic is missing."""
|
||||||
@ -458,6 +466,30 @@ async def test_service_call_with_ascii_qos_retain_flags(hass, mqtt_mock):
|
|||||||
assert not mqtt_mock.async_publish.call_args[0][3]
|
assert not mqtt_mock.async_publish.call_args[0][3]
|
||||||
|
|
||||||
|
|
||||||
|
async def test_publish_function_with_bad_encoding_conditions(hass, caplog):
|
||||||
|
"""Test internal publish function with bas use cases."""
|
||||||
|
await mqtt.async_publish(
|
||||||
|
hass, "some-topic", "test-payload", qos=0, retain=False, encoding=None
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
"Can't pass-through payload for publishing test-payload on some-topic with no encoding set, need 'bytes' got <class 'str'>"
|
||||||
|
in caplog.text
|
||||||
|
)
|
||||||
|
caplog.clear()
|
||||||
|
await mqtt.async_publish(
|
||||||
|
hass,
|
||||||
|
"some-topic",
|
||||||
|
"test-payload",
|
||||||
|
qos=0,
|
||||||
|
retain=False,
|
||||||
|
encoding="invalid_encoding",
|
||||||
|
)
|
||||||
|
assert (
|
||||||
|
"Can't encode payload for publishing test-payload on some-topic with encoding invalid_encoding"
|
||||||
|
in caplog.text
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_validate_topic():
|
def test_validate_topic():
|
||||||
"""Test topic name/filter validation."""
|
"""Test topic name/filter validation."""
|
||||||
# Invalid UTF-8, must not contain U+D800 to U+DFFF.
|
# Invalid UTF-8, must not contain U+D800 to U+DFFF.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user