Add mysensors binary sensor tests (#84580)

This commit is contained in:
Martin Hjelmare 2022-12-26 20:19:15 +01:00 committed by GitHub
parent cf92d8c536
commit 101118a60e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 85 additions and 1 deletions

View File

@ -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

View File

@ -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."""

View File

@ -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
}
}

View File

@ -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"