Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use parametrize in tests for Shelly boolean virtual component #121895

Merged
merged 1 commit into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 11 additions & 32 deletions tests/components/shelly/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,25 @@ async def test_rpc_restored_sleeping_binary_sensor_no_last_state(
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,
entity_registry: er.EntityRegistry,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
name: str | None,
entity_id: str,
) -> None:
"""Test a virtual binary sensor for RPC device."""
config = deepcopy(mock_rpc_device.config)
config["boolean:203"] = {
"name": "Virtual binary sensor",
"name": name,
"meta": {"ui": {"view": "label"}},
}
monkeypatch.setattr(mock_rpc_device, "config", config)
Expand All @@ -375,8 +384,6 @@ async def test_rpc_device_virtual_binary_sensor_with_name(
status["boolean:203"] = {"value": True}
monkeypatch.setattr(mock_rpc_device, "status", status)

entity_id = "binary_sensor.test_name_virtual_binary_sensor"

await init_integration(hass, 3)

state = hass.states.get(entity_id)
Expand All @@ -392,34 +399,6 @@ async def test_rpc_device_virtual_binary_sensor_with_name(
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(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
Expand Down
43 changes: 11 additions & 32 deletions tests/components/shelly/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,16 +433,25 @@ async def test_wall_display_relay_mode(
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,
entity_registry: er.EntityRegistry,
mock_rpc_device: Mock,
monkeypatch: pytest.MonkeyPatch,
name: str | None,
entity_id: str,
) -> None:
"""Test a virtual switch for RPC device."""
config = deepcopy(mock_rpc_device.config)
config["boolean:200"] = {
"name": "Virtual switch",
"name": name,
"meta": {"ui": {"view": "toggle"}},
}
monkeypatch.setattr(mock_rpc_device, "config", config)
Expand All @@ -451,8 +460,6 @@ async def test_rpc_device_virtual_switch_with_name(
status["boolean:200"] = {"value": True}
monkeypatch.setattr(mock_rpc_device, "status", status)

entity_id = "switch.test_name_virtual_switch"

await init_integration(hass, 3)

state = hass.states.get(entity_id)
Expand Down Expand Up @@ -484,34 +491,6 @@ async def test_rpc_device_virtual_switch_with_name(
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(
hass: HomeAssistant,
mock_rpc_device: Mock,
Expand Down