Add mysensors notify tests (#67634)

This commit is contained in:
Martin Hjelmare 2022-03-04 14:47:27 +01:00 committed by GitHub
parent 624c3be3ae
commit f91a809350
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 1 deletions

View file

@ -736,7 +736,6 @@ omit =
homeassistant/components/mysensors/handler.py
homeassistant/components/mysensors/helpers.py
homeassistant/components/mysensors/light.py
homeassistant/components/mysensors/notify.py
homeassistant/components/mysensors/switch.py
homeassistant/components/mystrom/binary_sensor.py
homeassistant/components/mystrom/light.py

View file

@ -278,3 +278,17 @@ def temperature_sensor(
nodes = update_gateway_nodes(gateway_nodes, temperature_sensor_state)
node = nodes[1]
return node
@pytest.fixture(name="text_node_state", scope="session")
def text_node_state_fixture() -> dict:
"""Load the text node state."""
return load_nodes_state("mysensors/text_node_state.json")
@pytest.fixture
def text_node(gateway_nodes: dict[int, Sensor], text_node_state: dict) -> Sensor:
"""Load the text child node."""
nodes = update_gateway_nodes(gateway_nodes, text_node_state)
node = nodes[1]
return node

View file

@ -0,0 +1,21 @@
{
"1": {
"sensor_id": 1,
"children": {
"1": {
"id": 1,
"type": 36,
"description": "",
"values": {
"47": "test"
}
}
},
"type": 17,
"sketch_name": "Text Node",
"sketch_version": "1.0",
"battery_level": 0,
"protocol_version": "2.3.2",
"heartbeat": 0
}
}

View file

@ -0,0 +1,95 @@
"""Provide tests for mysensors notify platform."""
from __future__ import annotations
from collections.abc import Callable
from unittest.mock import MagicMock, call
from mysensors.sensor import Sensor
from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def test_text_type(
hass: HomeAssistant,
text_node: Sensor,
transport_write: MagicMock,
integration: MockConfigEntry,
) -> None:
"""Test a text type child."""
# Test without target.
await hass.services.async_call(
NOTIFY_DOMAIN, "mysensors", {"message": "Hello World"}, blocking=True
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;0;47;Hello World\n")
# Test with target.
await hass.services.async_call(
NOTIFY_DOMAIN,
"mysensors",
{"message": "Hello", "target": "Text Node 1 1"},
blocking=True,
)
assert transport_write.call_count == 2
assert transport_write.call_args == call("1;1;1;0;47;Hello\n")
transport_write.reset_mock()
# Test a message longer than 25 characters.
await hass.services.async_call(
NOTIFY_DOMAIN,
"mysensors",
{
"message": "This is a long message that will be split",
"target": "Text Node 1 1",
},
blocking=True,
)
assert transport_write.call_count == 2
assert transport_write.call_args_list == [
call("1;1;1;0;47;This is a long message th\n"),
call("1;1;1;0;47;at will be split\n"),
]
async def test_text_type_discovery(
hass: HomeAssistant,
text_node: Sensor,
transport_write: MagicMock,
receive_message: Callable[[str], None],
) -> None:
"""Test text type discovery."""
receive_message("1;2;0;0;36;\n")
receive_message("1;2;1;0;47;test\n")
receive_message("1;2;1;0;47;test2\n") # Test that more than one set message works.
await hass.async_block_till_done()
# Test targeting the discovered child.
await hass.services.async_call(
NOTIFY_DOMAIN,
"mysensors",
{"message": "Hello", "target": "Text Node 1 2"},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;2;1;0;47;Hello\n")
transport_write.reset_mock()
# Test targeting all notify children.
await hass.services.async_call(
NOTIFY_DOMAIN, "mysensors", {"message": "Hello World"}, blocking=True
)
assert transport_write.call_count == 2
assert transport_write.call_args_list == [
call("1;1;1;0;47;Hello World\n"),
call("1;2;1;0;47;Hello World\n"),
]