From 101118a60e6019917342a8bc76826086eccab2c8 Mon Sep 17 00:00:00 2001 From: Martin Hjelmare Date: Mon, 26 Dec 2022 20:19:15 +0100 Subject: [PATCH] Add mysensors binary sensor tests (#84580) --- .coveragerc | 1 - tests/components/mysensors/conftest.py | 15 ++++++ .../mysensors/fixtures/door_sensor_state.json | 21 ++++++++ .../mysensors/test_binary_sensor.py | 49 +++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tests/components/mysensors/fixtures/door_sensor_state.json create mode 100644 tests/components/mysensors/test_binary_sensor.py diff --git a/.coveragerc b/.coveragerc index 06e0b61d9d1..c7377e21e68 100644 --- a/.coveragerc +++ b/.coveragerc @@ -797,7 +797,6 @@ omit = homeassistant/components/myq/cover.py homeassistant/components/myq/light.py homeassistant/components/mysensors/__init__.py - homeassistant/components/mysensors/binary_sensor.py homeassistant/components/mysensors/climate.py homeassistant/components/mysensors/const.py homeassistant/components/mysensors/cover.py diff --git a/tests/components/mysensors/conftest.py b/tests/components/mysensors/conftest.py index 7c73ce1a389..e8dc59cf526 100644 --- a/tests/components/mysensors/conftest.py +++ b/tests/components/mysensors/conftest.py @@ -2,6 +2,7 @@ from __future__ import annotations from collections.abc import AsyncGenerator, Callable, Generator +from copy import deepcopy import json from typing import Any from unittest.mock import AsyncMock, MagicMock, patch @@ -189,6 +190,20 @@ def update_gateway_nodes( return nodes +@pytest.fixture(name="door_sensor_state", scope="session") +def door_sensor_state_fixture() -> dict: + """Load the door sensor state.""" + return load_nodes_state("mysensors/door_sensor_state.json") + + +@pytest.fixture +def door_sensor(gateway_nodes: dict[int, Sensor], door_sensor_state: dict) -> Sensor: + """Load the door sensor.""" + nodes = update_gateway_nodes(gateway_nodes, deepcopy(door_sensor_state)) + node = nodes[1] + return node + + @pytest.fixture(name="gps_sensor_state", scope="session") def gps_sensor_state_fixture() -> dict: """Load the gps sensor state.""" diff --git a/tests/components/mysensors/fixtures/door_sensor_state.json b/tests/components/mysensors/fixtures/door_sensor_state.json new file mode 100644 index 00000000000..0034ec6c92c --- /dev/null +++ b/tests/components/mysensors/fixtures/door_sensor_state.json @@ -0,0 +1,21 @@ +{ + "1": { + "sensor_id": 1, + "children": { + "1": { + "id": 1, + "type": 0, + "description": "", + "values": { + "16": "0" + } + } + }, + "type": 17, + "sketch_name": "Door Sensor", + "sketch_version": "1.0", + "battery_level": 0, + "protocol_version": "2.3.2", + "heartbeat": 0 + } +} diff --git a/tests/components/mysensors/test_binary_sensor.py b/tests/components/mysensors/test_binary_sensor.py new file mode 100644 index 00000000000..652a4a0bf66 --- /dev/null +++ b/tests/components/mysensors/test_binary_sensor.py @@ -0,0 +1,49 @@ +"""Provide tests for mysensors binary sensor platform.""" +from __future__ import annotations + +from collections.abc import Callable +from unittest.mock import MagicMock + +from mysensors.sensor import Sensor + +from homeassistant.components.binary_sensor import BinarySensorDeviceClass +from homeassistant.const import ATTR_DEVICE_CLASS +from homeassistant.core import HomeAssistant + + +async def test_door_sensor( + hass: HomeAssistant, + door_sensor: Sensor, + receive_message: Callable[[str], None], + transport_write: MagicMock, +) -> None: + """Test a door sensor.""" + entity_id = "binary_sensor.door_sensor_1_1" + + state = hass.states.get(entity_id) + + assert state + assert state.state == "off" + assert state.attributes[ATTR_DEVICE_CLASS] == BinarySensorDeviceClass.DOOR + + receive_message("1;1;1;0;16;1\n") + # the integration adds multiple jobs to do the update currently + await hass.async_block_till_done() + await hass.async_block_till_done() + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + + assert state + assert state.state == "on" + + receive_message("1;1;1;0;16;0\n") + # the integration adds multiple jobs to do the update currently + await hass.async_block_till_done() + await hass.async_block_till_done() + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + + assert state + assert state.state == "off"