diff --git a/.coveragerc b/.coveragerc index 11db4118ebd..4144b38cc0f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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 diff --git a/tests/components/mysensors/conftest.py b/tests/components/mysensors/conftest.py index 7e38d42f011..54ae88cdccb 100644 --- a/tests/components/mysensors/conftest.py +++ b/tests/components/mysensors/conftest.py @@ -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 diff --git a/tests/components/mysensors/fixtures/text_node_state.json b/tests/components/mysensors/fixtures/text_node_state.json new file mode 100644 index 00000000000..a36d78b72a8 --- /dev/null +++ b/tests/components/mysensors/fixtures/text_node_state.json @@ -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 + } +} diff --git a/tests/components/mysensors/test_notify.py b/tests/components/mysensors/test_notify.py new file mode 100644 index 00000000000..e96b463cc78 --- /dev/null +++ b/tests/components/mysensors/test_notify.py @@ -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"), + ]