Use parametrize in tests for Shelly boolean virtual component (#121895)

Use parametrize in tests

Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com>
This commit is contained in:
Maciej Bieniek 2024-07-13 14:27:17 +02:00 committed by GitHub
parent 62613af033
commit a34858a567
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 64 deletions

View File

@ -357,16 +357,25 @@ async def test_rpc_restored_sleeping_binary_sensor_no_last_state(
assert hass.states.get(entity_id).state == STATE_OFF assert hass.states.get(entity_id).state == STATE_OFF
async def test_rpc_device_virtual_binary_sensor_with_name( @pytest.mark.parametrize(
("name", "entity_id"),
[
("Virtual binary sensor", "binary_sensor.test_name_virtual_binary_sensor"),
(None, "binary_sensor.test_name_boolean_203"),
],
)
async def test_rpc_device_virtual_binary_sensor(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mock_rpc_device: Mock, mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
name: str | None,
entity_id: str,
) -> None: ) -> None:
"""Test a virtual binary sensor for RPC device.""" """Test a virtual binary sensor for RPC device."""
config = deepcopy(mock_rpc_device.config) config = deepcopy(mock_rpc_device.config)
config["boolean:203"] = { config["boolean:203"] = {
"name": "Virtual binary sensor", "name": name,
"meta": {"ui": {"view": "label"}}, "meta": {"ui": {"view": "label"}},
} }
monkeypatch.setattr(mock_rpc_device, "config", config) monkeypatch.setattr(mock_rpc_device, "config", config)
@ -375,8 +384,6 @@ async def test_rpc_device_virtual_binary_sensor_with_name(
status["boolean:203"] = {"value": True} status["boolean:203"] = {"value": True}
monkeypatch.setattr(mock_rpc_device, "status", status) monkeypatch.setattr(mock_rpc_device, "status", status)
entity_id = "binary_sensor.test_name_virtual_binary_sensor"
await init_integration(hass, 3) await init_integration(hass, 3)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
@ -392,34 +399,6 @@ async def test_rpc_device_virtual_binary_sensor_with_name(
assert hass.states.get(entity_id).state == STATE_OFF assert hass.states.get(entity_id).state == STATE_OFF
async def test_rpc_device_virtual_binary_sensor_without_name(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test a virtual binary sensor for RPC device."""
config = deepcopy(mock_rpc_device.config)
config["boolean:203"] = {"name": None, "meta": {"ui": {"view": "label"}}}
monkeypatch.setattr(mock_rpc_device, "config", config)
status = deepcopy(mock_rpc_device.status)
status["boolean:203"] = {"value": True}
monkeypatch.setattr(mock_rpc_device, "status", status)
entity_id = "binary_sensor.test_name_boolean_203"
await init_integration(hass, 3)
state = hass.states.get(entity_id)
assert state
assert state.state == STATE_ON
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == "123456789ABC-boolean:203-boolean"
async def test_rpc_remove_virtual_binary_sensor_when_mode_toggle( async def test_rpc_remove_virtual_binary_sensor_when_mode_toggle(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,

View File

@ -433,16 +433,25 @@ async def test_wall_display_relay_mode(
assert entry.unique_id == "123456789ABC-switch:0" assert entry.unique_id == "123456789ABC-switch:0"
async def test_rpc_device_virtual_switch_with_name( @pytest.mark.parametrize(
("name", "entity_id"),
[
("Virtual switch", "switch.test_name_virtual_switch"),
(None, "switch.test_name_boolean_200"),
],
)
async def test_rpc_device_virtual_switch(
hass: HomeAssistant, hass: HomeAssistant,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mock_rpc_device: Mock, mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch, monkeypatch: pytest.MonkeyPatch,
name: str | None,
entity_id: str,
) -> None: ) -> None:
"""Test a virtual switch for RPC device.""" """Test a virtual switch for RPC device."""
config = deepcopy(mock_rpc_device.config) config = deepcopy(mock_rpc_device.config)
config["boolean:200"] = { config["boolean:200"] = {
"name": "Virtual switch", "name": name,
"meta": {"ui": {"view": "toggle"}}, "meta": {"ui": {"view": "toggle"}},
} }
monkeypatch.setattr(mock_rpc_device, "config", config) monkeypatch.setattr(mock_rpc_device, "config", config)
@ -451,8 +460,6 @@ async def test_rpc_device_virtual_switch_with_name(
status["boolean:200"] = {"value": True} status["boolean:200"] = {"value": True}
monkeypatch.setattr(mock_rpc_device, "status", status) monkeypatch.setattr(mock_rpc_device, "status", status)
entity_id = "switch.test_name_virtual_switch"
await init_integration(hass, 3) await init_integration(hass, 3)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
@ -484,34 +491,6 @@ async def test_rpc_device_virtual_switch_with_name(
assert hass.states.get(entity_id).state == STATE_ON assert hass.states.get(entity_id).state == STATE_ON
async def test_rpc_device_virtual_switch_without_name(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test a virtual switch for RPC device."""
config = deepcopy(mock_rpc_device.config)
config["boolean:200"] = {"name": None, "meta": {"ui": {"view": "toggle"}}}
monkeypatch.setattr(mock_rpc_device, "config", config)
status = deepcopy(mock_rpc_device.status)
status["boolean:200"] = {"value": True}
monkeypatch.setattr(mock_rpc_device, "status", status)
entity_id = "switch.test_name_boolean_200"
await init_integration(hass, 3)
state = hass.states.get(entity_id)
assert state
assert state.state == STATE_ON
entry = entity_registry.async_get(entity_id)
assert entry
assert entry.unique_id == "123456789ABC-boolean:200-boolean"
async def test_rpc_device_virtual_binary_sensor( async def test_rpc_device_virtual_binary_sensor(
hass: HomeAssistant, hass: HomeAssistant,
mock_rpc_device: Mock, mock_rpc_device: Mock,