Add mysensors light tests (#84606)

This commit is contained in:
Martin Hjelmare 2022-12-27 15:25:22 +01:00 committed by GitHub
parent 76f92cc985
commit dea513cdbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 458 additions and 1 deletions

View File

@ -798,7 +798,6 @@ omit =
homeassistant/components/myq/light.py homeassistant/components/myq/light.py
homeassistant/components/mysensors/__init__.py homeassistant/components/mysensors/__init__.py
homeassistant/components/mysensors/climate.py homeassistant/components/mysensors/climate.py
homeassistant/components/mysensors/const.py
homeassistant/components/mysensors/cover.py homeassistant/components/mysensors/cover.py
homeassistant/components/mysensors/device.py homeassistant/components/mysensors/device.py
homeassistant/components/mysensors/gateway.py homeassistant/components/mysensors/gateway.py

View File

@ -218,6 +218,20 @@ def gps_sensor(gateway_nodes: dict[int, Sensor], gps_sensor_state: dict) -> Sens
return node return node
@pytest.fixture(name="dimmer_node_state", scope="session")
def dimmer_node_state_fixture() -> dict:
"""Load the dimmer node state."""
return load_nodes_state("mysensors/dimmer_node_state.json")
@pytest.fixture
def dimmer_node(gateway_nodes: dict[int, Sensor], dimmer_node_state: dict) -> Sensor:
"""Load the dimmer child node."""
nodes = update_gateway_nodes(gateway_nodes, deepcopy(dimmer_node_state))
node = nodes[1]
return node
@pytest.fixture(name="power_sensor_state", scope="session") @pytest.fixture(name="power_sensor_state", scope="session")
def power_sensor_state_fixture() -> dict: def power_sensor_state_fixture() -> dict:
"""Load the power sensor state.""" """Load the power sensor state."""
@ -232,6 +246,34 @@ def power_sensor(gateway_nodes: dict[int, Sensor], power_sensor_state: dict) ->
return node return node
@pytest.fixture(name="rgb_node_state", scope="session")
def rgb_node_state_fixture() -> dict:
"""Load the rgb node state."""
return load_nodes_state("mysensors/rgb_node_state.json")
@pytest.fixture
def rgb_node(gateway_nodes: dict[int, Sensor], rgb_node_state: dict) -> Sensor:
"""Load the rgb child node."""
nodes = update_gateway_nodes(gateway_nodes, deepcopy(rgb_node_state))
node = nodes[1]
return node
@pytest.fixture(name="rgbw_node_state", scope="session")
def rgbw_node_state_fixture() -> dict:
"""Load the rgbw node state."""
return load_nodes_state("mysensors/rgbw_node_state.json")
@pytest.fixture
def rgbw_node(gateway_nodes: dict[int, Sensor], rgbw_node_state: dict) -> Sensor:
"""Load the rgbw child node."""
nodes = update_gateway_nodes(gateway_nodes, deepcopy(rgbw_node_state))
node = nodes[1]
return node
@pytest.fixture(name="energy_sensor_state", scope="session") @pytest.fixture(name="energy_sensor_state", scope="session")
def energy_sensor_state_fixture() -> dict: def energy_sensor_state_fixture() -> dict:
"""Load the energy sensor state.""" """Load the energy sensor state."""

View File

@ -0,0 +1,22 @@
{
"1": {
"sensor_id": 1,
"children": {
"1": {
"id": 1,
"type": 4,
"description": "",
"values": {
"2": "0",
"3": "0"
}
}
},
"type": 17,
"sketch_name": "Dimmer Node",
"sketch_version": "1.0",
"battery_level": 0,
"protocol_version": "2.3.2",
"heartbeat": 0
}
}

View File

@ -0,0 +1,23 @@
{
"1": {
"sensor_id": 1,
"children": {
"1": {
"id": 1,
"type": 26,
"description": "",
"values": {
"2": "0",
"3": "0",
"40": "ffffff"
}
}
},
"type": 17,
"sketch_name": "RGB Node",
"sketch_version": "1.0",
"battery_level": 0,
"protocol_version": "2.3.2",
"heartbeat": 0
}
}

View File

@ -0,0 +1,23 @@
{
"1": {
"sensor_id": 1,
"children": {
"1": {
"id": 1,
"type": 27,
"description": "",
"values": {
"2": "0",
"3": "0",
"41": "ffffffff"
}
}
},
"type": 17,
"sketch_name": "RGBW Node",
"sketch_version": "1.0",
"battery_level": 0,
"protocol_version": "2.3.2",
"heartbeat": 0
}
}

View File

@ -0,0 +1,348 @@
"""Provide tests for mysensors light platform."""
from __future__ import annotations
from collections.abc import Callable
from unittest.mock import MagicMock, call
from mysensors.sensor import Sensor
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_RGB_COLOR,
ATTR_RGBW_COLOR,
DOMAIN as LIGHT_DOMAIN,
)
from homeassistant.core import HomeAssistant
async def test_dimmer_node(
hass: HomeAssistant,
dimmer_node: Sensor,
receive_message: Callable[[str], None],
transport_write: MagicMock,
) -> None:
"""Test a dimmer node."""
entity_id = "light.dimmer_node_1_1"
state = hass.states.get(entity_id)
assert state
assert state.state == "off"
# Test turn on
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{"entity_id": entity_id},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;2;1\n")
receive_message("1;1;1;0;2;1\n")
receive_message("1;1;1;0;3;100\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"
assert state.attributes[ATTR_BRIGHTNESS] == 255
transport_write.reset_mock()
# Test turn on brightness
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{"entity_id": entity_id, "brightness": 128},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;3;50\n")
receive_message("1;1;1;0;2;1\n")
receive_message("1;1;1;0;3;50\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"
assert state.attributes[ATTR_BRIGHTNESS] == 128
transport_write.reset_mock()
# Test turn off
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_off",
{"entity_id": entity_id},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;2;0\n")
receive_message("1;1;1;0;2;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"
async def test_rgb_node(
hass: HomeAssistant,
rgb_node: Sensor,
receive_message: Callable[[str], None],
transport_write: MagicMock,
) -> None:
"""Test a rgb node."""
entity_id = "light.rgb_node_1_1"
state = hass.states.get(entity_id)
assert state
assert state.state == "off"
# Test turn on
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{"entity_id": entity_id},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;2;1\n")
receive_message("1;1;1;0;2;1\n")
receive_message("1;1;1;0;3;100\n")
receive_message("1;1;1;0;40;ffffff\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"
assert state.attributes[ATTR_BRIGHTNESS] == 255
assert state.attributes[ATTR_RGB_COLOR] == (255, 255, 255)
transport_write.reset_mock()
# Test turn on brightness
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{"entity_id": entity_id, "brightness": 128},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;3;50\n")
receive_message("1;1;1;0;2;1\n")
receive_message("1;1;1;0;3;50\n")
receive_message("1;1;1;0;40;ffffff\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"
assert state.attributes[ATTR_BRIGHTNESS] == 128
assert state.attributes[ATTR_RGB_COLOR] == (255, 255, 255)
transport_write.reset_mock()
# Test turn off
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_off",
{"entity_id": entity_id},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;2;0\n")
receive_message("1;1;1;0;2;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"
transport_write.reset_mock()
# Test turn on rgb
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{"entity_id": entity_id, ATTR_RGB_COLOR: (255, 0, 0)},
blocking=True,
)
assert transport_write.call_count == 2
assert transport_write.call_args_list[0] == call("1;1;1;1;2;1\n")
assert transport_write.call_args_list[1] == call("1;1;1;1;40;ff0000\n")
receive_message("1;1;1;0;2;1\n")
receive_message("1;1;1;0;3;50\n")
receive_message("1;1;1;0;40;ff0000\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"
assert state.attributes[ATTR_BRIGHTNESS] == 128
assert state.attributes[ATTR_RGB_COLOR] == (255, 0, 0)
async def test_rgbw_node(
hass: HomeAssistant,
rgbw_node: Sensor,
receive_message: Callable[[str], None],
transport_write: MagicMock,
) -> None:
"""Test a rgbw node."""
entity_id = "light.rgbw_node_1_1"
state = hass.states.get(entity_id)
assert state
assert state.state == "off"
# Test turn on
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{"entity_id": entity_id},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;2;1\n")
receive_message("1;1;1;0;2;1\n")
receive_message("1;1;1;0;3;100\n")
receive_message("1;1;1;0;41;ffffffff\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"
assert state.attributes[ATTR_BRIGHTNESS] == 255
assert state.attributes[ATTR_RGBW_COLOR] == (255, 255, 255, 255)
transport_write.reset_mock()
# Test turn on brightness
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{"entity_id": entity_id, "brightness": 128},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;3;50\n")
receive_message("1;1;1;0;2;1\n")
receive_message("1;1;1;0;3;50\n")
receive_message("1;1;1;0;41;ffffffff\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"
assert state.attributes[ATTR_BRIGHTNESS] == 128
assert state.attributes[ATTR_RGBW_COLOR] == (255, 255, 255, 255)
transport_write.reset_mock()
# Test turn off
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_off",
{"entity_id": entity_id},
blocking=True,
)
assert transport_write.call_count == 1
assert transport_write.call_args == call("1;1;1;1;2;0\n")
receive_message("1;1;1;0;2;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"
transport_write.reset_mock()
# Test turn on rgbw
await hass.services.async_call(
LIGHT_DOMAIN,
"turn_on",
{"entity_id": entity_id, ATTR_RGBW_COLOR: (255, 0, 0, 0)},
blocking=True,
)
assert transport_write.call_count == 2
assert transport_write.call_args_list[0] == call("1;1;1;1;2;1\n")
assert transport_write.call_args_list[1] == call("1;1;1;1;41;ff000000\n")
receive_message("1;1;1;0;2;1\n")
receive_message("1;1;1;0;3;50\n")
receive_message("1;1;1;0;41;ff000000\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"
assert state.attributes[ATTR_BRIGHTNESS] == 128
assert state.attributes[ATTR_RGBW_COLOR] == (255, 0, 0, 0)