2021-05-21 07:56:47 +00:00
|
|
|
"""The tests for the Modbus fan component."""
|
|
|
|
from pymodbus.exceptions import ModbusException
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
|
|
|
|
from homeassistant.components.modbus.const import (
|
|
|
|
CALL_TYPE_COIL,
|
|
|
|
CALL_TYPE_DISCRETE,
|
|
|
|
CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
CALL_TYPE_REGISTER_INPUT,
|
|
|
|
CONF_FANS,
|
|
|
|
CONF_INPUT_TYPE,
|
2021-08-21 13:49:50 +00:00
|
|
|
CONF_LAZY_ERROR,
|
2021-05-21 07:56:47 +00:00
|
|
|
CONF_STATE_OFF,
|
|
|
|
CONF_STATE_ON,
|
|
|
|
CONF_VERIFY,
|
|
|
|
CONF_WRITE_TYPE,
|
|
|
|
MODBUS_DOMAIN,
|
|
|
|
)
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_ADDRESS,
|
|
|
|
CONF_COMMAND_OFF,
|
|
|
|
CONF_COMMAND_ON,
|
|
|
|
CONF_NAME,
|
2021-07-21 05:48:02 +00:00
|
|
|
CONF_SCAN_INTERVAL,
|
2021-05-21 07:56:47 +00:00
|
|
|
CONF_SLAVE,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
)
|
|
|
|
from homeassistant.core import State
|
2022-03-11 17:58:18 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2021-05-21 07:56:47 +00:00
|
|
|
|
2022-02-20 05:00:14 +00:00
|
|
|
from .conftest import TEST_ENTITY_NAME, ReadResult
|
2021-05-21 07:56:47 +00:00
|
|
|
|
2022-02-21 22:56:31 +00:00
|
|
|
ENTITY_ID = f"{FAN_DOMAIN}.{TEST_ENTITY_NAME}".replace(" ", "_")
|
|
|
|
ENTITY_ID2 = f"{ENTITY_ID}_2"
|
2021-05-21 07:56:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
}
|
|
|
|
]
|
2021-05-21 07:56:47 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_WRITE_TYPE: CALL_TYPE_COIL,
|
|
|
|
}
|
|
|
|
]
|
2021-05-21 07:56:47 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
CONF_COMMAND_OFF: 0x00,
|
|
|
|
CONF_COMMAND_ON: 0x01,
|
2021-08-21 13:49:50 +00:00
|
|
|
CONF_LAZY_ERROR: 10,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_VERIFY: {
|
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
CONF_ADDRESS: 1235,
|
|
|
|
CONF_STATE_OFF: 0,
|
|
|
|
CONF_STATE_ON: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]
|
2021-05-21 07:56:47 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
CONF_COMMAND_OFF: 0x00,
|
|
|
|
CONF_COMMAND_ON: 0x01,
|
|
|
|
CONF_VERIFY: {
|
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_REGISTER_INPUT,
|
|
|
|
CONF_ADDRESS: 1235,
|
|
|
|
CONF_STATE_OFF: 0,
|
|
|
|
CONF_STATE_ON: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]
|
2021-05-21 07:56:47 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
CONF_COMMAND_OFF: 0x00,
|
|
|
|
CONF_COMMAND_ON: 0x01,
|
|
|
|
CONF_VERIFY: {
|
|
|
|
CONF_INPUT_TYPE: CALL_TYPE_DISCRETE,
|
|
|
|
CONF_ADDRESS: 1235,
|
|
|
|
CONF_STATE_OFF: 0,
|
|
|
|
CONF_STATE_ON: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
]
|
2021-05-21 07:56:47 +00:00
|
|
|
},
|
|
|
|
{
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-17 10:27:05 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
CONF_COMMAND_OFF: 0x00,
|
|
|
|
CONF_COMMAND_ON: 0x01,
|
|
|
|
CONF_VERIFY: None,
|
|
|
|
}
|
|
|
|
]
|
2021-05-21 07:56:47 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
2021-06-17 10:27:05 +00:00
|
|
|
async def test_config_fan(hass, mock_modbus):
|
|
|
|
"""Run configuration test for fan."""
|
|
|
|
assert FAN_DOMAIN in hass.config.components
|
2021-05-21 07:56:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2021-08-22 17:59:38 +00:00
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_FANS: [
|
|
|
|
{
|
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
CONF_WRITE_TYPE: CALL_TYPE_COIL,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CONF_FANS: [
|
|
|
|
{
|
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_SLAVE: 1,
|
|
|
|
CONF_WRITE_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
2021-08-23 21:55:57 +00:00
|
|
|
"register_words,do_exception,config_addon,expected",
|
2021-05-21 07:56:47 +00:00
|
|
|
[
|
|
|
|
(
|
|
|
|
[0x00],
|
2021-08-23 21:55:57 +00:00
|
|
|
False,
|
2021-05-21 07:56:47 +00:00
|
|
|
{CONF_VERIFY: {}},
|
|
|
|
STATE_OFF,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[0x01],
|
2021-08-23 21:55:57 +00:00
|
|
|
False,
|
2021-05-21 07:56:47 +00:00
|
|
|
{CONF_VERIFY: {}},
|
|
|
|
STATE_ON,
|
|
|
|
),
|
|
|
|
(
|
|
|
|
[0xFE],
|
2021-08-23 21:55:57 +00:00
|
|
|
False,
|
2021-05-21 07:56:47 +00:00
|
|
|
{CONF_VERIFY: {}},
|
|
|
|
STATE_OFF,
|
|
|
|
),
|
|
|
|
(
|
2021-08-23 21:55:57 +00:00
|
|
|
[0x00],
|
|
|
|
True,
|
2021-05-21 07:56:47 +00:00
|
|
|
{CONF_VERIFY: {}},
|
|
|
|
STATE_UNAVAILABLE,
|
|
|
|
),
|
|
|
|
(
|
2021-08-23 21:55:57 +00:00
|
|
|
[0x00],
|
|
|
|
True,
|
2021-08-22 17:59:38 +00:00
|
|
|
None,
|
2021-05-21 07:56:47 +00:00
|
|
|
STATE_OFF,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2021-08-23 21:55:57 +00:00
|
|
|
async def test_all_fan(hass, mock_do_cycle, expected):
|
2021-05-21 07:56:47 +00:00
|
|
|
"""Run test for given config."""
|
2021-08-22 17:59:38 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == expected
|
2021-05-21 07:56:47 +00:00
|
|
|
|
|
|
|
|
2021-06-29 05:21:04 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"mock_test_state",
|
2021-06-30 12:34:33 +00:00
|
|
|
[(State(ENTITY_ID, STATE_ON),)],
|
2021-06-29 05:21:04 +00:00
|
|
|
indirect=True,
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-06-29 05:21:04 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
2021-07-21 05:48:02 +00:00
|
|
|
CONF_SCAN_INTERVAL: 0,
|
2021-06-29 05:21:04 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_restore_state_fan(hass, mock_test_state, mock_modbus):
|
2021-05-21 07:56:47 +00:00
|
|
|
"""Run test for fan restore state."""
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_ON
|
2021-05-21 07:56:47 +00:00
|
|
|
|
|
|
|
|
2022-02-20 05:00:14 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
2021-05-21 07:56:47 +00:00
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-05-21 07:56:47 +00:00
|
|
|
CONF_ADDRESS: 17,
|
|
|
|
CONF_WRITE_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
2021-07-21 05:48:02 +00:00
|
|
|
CONF_SCAN_INTERVAL: 0,
|
2021-05-21 07:56:47 +00:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 22:56:31 +00:00
|
|
|
CONF_NAME: f"{TEST_ENTITY_NAME} 2",
|
2021-08-19 07:37:31 +00:00
|
|
|
CONF_ADDRESS: 18,
|
2021-05-21 07:56:47 +00:00
|
|
|
CONF_WRITE_TYPE: CALL_TYPE_REGISTER_HOLDING,
|
2021-07-21 05:48:02 +00:00
|
|
|
CONF_SCAN_INTERVAL: 0,
|
2021-05-21 07:56:47 +00:00
|
|
|
CONF_VERIFY: {},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2022-02-20 05:00:14 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_fan_service_turn(hass, caplog, mock_modbus):
|
|
|
|
"""Run test for service turn_on/turn_off."""
|
|
|
|
|
2021-05-21 07:56:47 +00:00
|
|
|
assert MODBUS_DOMAIN in hass.config.components
|
|
|
|
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_OFF
|
2021-05-21 07:56:47 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"fan", "turn_on", service_data={"entity_id": ENTITY_ID}
|
2021-05-21 07:56:47 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_ON
|
2021-05-21 07:56:47 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"fan", "turn_off", service_data={"entity_id": ENTITY_ID}
|
2021-05-21 07:56:47 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_OFF
|
2021-05-21 07:56:47 +00:00
|
|
|
|
2022-02-20 05:00:14 +00:00
|
|
|
mock_modbus.read_holding_registers.return_value = ReadResult([0x01])
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID2).state == STATE_OFF
|
2021-05-21 07:56:47 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"fan", "turn_on", service_data={"entity_id": ENTITY_ID2}
|
2021-05-21 07:56:47 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID2).state == STATE_ON
|
2022-02-20 05:00:14 +00:00
|
|
|
mock_modbus.read_holding_registers.return_value = ReadResult([0x00])
|
2021-05-21 07:56:47 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"fan", "turn_off", service_data={"entity_id": ENTITY_ID2}
|
2021-05-21 07:56:47 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID2).state == STATE_OFF
|
2021-05-21 07:56:47 +00:00
|
|
|
|
2022-02-20 05:00:14 +00:00
|
|
|
mock_modbus.write_register.side_effect = ModbusException("fail write_")
|
2021-05-21 07:56:47 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"fan", "turn_on", service_data={"entity_id": ENTITY_ID2}
|
2021-05-21 07:56:47 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID2).state == STATE_UNAVAILABLE
|
2022-02-20 05:00:14 +00:00
|
|
|
mock_modbus.write_coil.side_effect = ModbusException("fail write_")
|
2021-05-21 07:56:47 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"fan", "turn_off", service_data={"entity_id": ENTITY_ID}
|
2021-05-21 07:56:47 +00:00
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2021-06-30 12:34:33 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_UNAVAILABLE
|
2021-05-21 07:56:47 +00:00
|
|
|
|
|
|
|
|
2021-08-11 10:42:28 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"do_config",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
CONF_FANS: [
|
|
|
|
{
|
2021-08-17 18:43:27 +00:00
|
|
|
CONF_NAME: TEST_ENTITY_NAME,
|
2021-08-11 10:42:28 +00:00
|
|
|
CONF_ADDRESS: 1234,
|
|
|
|
CONF_WRITE_TYPE: CALL_TYPE_COIL,
|
|
|
|
CONF_VERIFY: {},
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_service_fan_update(hass, mock_modbus, mock_ha):
|
2021-05-21 07:56:47 +00:00
|
|
|
"""Run test for service homeassistant.update_entity."""
|
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"homeassistant", "update_entity", {"entity_id": ENTITY_ID}, blocking=True
|
2021-05-21 07:56:47 +00:00
|
|
|
)
|
2021-08-11 10:42:28 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_OFF
|
|
|
|
mock_modbus.read_coils.return_value = ReadResult([0x01])
|
2021-05-21 07:56:47 +00:00
|
|
|
await hass.services.async_call(
|
2021-06-30 12:34:33 +00:00
|
|
|
"homeassistant", "update_entity", {"entity_id": ENTITY_ID}, blocking=True
|
2021-05-21 07:56:47 +00:00
|
|
|
)
|
2021-08-11 10:42:28 +00:00
|
|
|
assert hass.states.get(ENTITY_ID).state == STATE_ON
|
2022-03-11 17:58:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_no_discovery_info_fan(hass, caplog):
|
|
|
|
"""Test setup without discovery info."""
|
|
|
|
assert FAN_DOMAIN not in hass.config.components
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass,
|
|
|
|
FAN_DOMAIN,
|
|
|
|
{FAN_DOMAIN: {"platform": MODBUS_DOMAIN}},
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert FAN_DOMAIN in hass.config.components
|