Files
core/tests/components/plugwise/test_switch.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

205 lines
6.6 KiB
Python
Raw Normal View History

2020-09-08 08:37:44 +02:00
"""Tests for the Plugwise switch integration."""
from unittest.mock import MagicMock
from plugwise.exceptions import PlugwiseException
2022-02-10 09:53:26 +01:00
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.plugwise.const import DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.const import (
2024-12-10 16:43:08 +01:00
ATTR_ENTITY_ID,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
2024-09-24 18:11:17 +02:00
STATE_OFF,
STATE_ON,
)
from homeassistant.core import HomeAssistant
2022-02-10 09:53:26 +01:00
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
2020-09-08 08:37:44 +02:00
from tests.common import MockConfigEntry, snapshot_platform
2020-09-08 08:37:44 +02:00
@pytest.mark.parametrize("platforms", [(SWITCH_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_adam_switch_snapshot(
hass: HomeAssistant,
mock_smile_adam: MagicMock,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test Adam switch snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)
async def test_adam_climate_switch_changes(
hass: HomeAssistant, mock_smile_adam: MagicMock, init_integration: MockConfigEntry
) -> None:
2020-09-08 08:37:44 +02:00
"""Test changing of climate related switch entities."""
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
2024-12-10 16:43:08 +01:00
{ATTR_ENTITY_ID: "switch.cv_pomp_relay"},
2020-09-08 08:37:44 +02:00
blocking=True,
)
2022-02-10 09:53:26 +01:00
assert mock_smile_adam.set_switch_state.call_count == 1
mock_smile_adam.set_switch_state.assert_called_with(
2024-09-24 18:11:17 +02:00
"78d1126fc4c743db81b61c20e88342a7", None, "relay", STATE_OFF
2022-02-10 09:53:26 +01:00
)
2020-09-08 08:37:44 +02:00
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TOGGLE,
2024-12-10 16:43:08 +01:00
{ATTR_ENTITY_ID: "switch.fibaro_hc2_relay"},
2020-09-08 08:37:44 +02:00
blocking=True,
)
2022-02-10 09:53:26 +01:00
assert mock_smile_adam.set_switch_state.call_count == 2
mock_smile_adam.set_switch_state.assert_called_with(
2024-09-24 18:11:17 +02:00
"a28f588dc4a049a483fd03a30361ad3a", None, "relay", STATE_OFF
2022-02-10 09:53:26 +01:00
)
2020-09-08 08:37:44 +02:00
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
2024-12-10 16:43:08 +01:00
{ATTR_ENTITY_ID: "switch.fibaro_hc2_relay"},
2020-09-08 08:37:44 +02:00
blocking=True,
)
2022-02-10 09:53:26 +01:00
assert mock_smile_adam.set_switch_state.call_count == 3
mock_smile_adam.set_switch_state.assert_called_with(
2024-09-24 18:11:17 +02:00
"a28f588dc4a049a483fd03a30361ad3a", None, "relay", STATE_ON
2022-02-10 09:53:26 +01:00
)
async def test_adam_climate_switch_negative_testing(
hass: HomeAssistant, mock_smile_adam: MagicMock, init_integration: MockConfigEntry
) -> None:
"""Test exceptions of climate related switch entities."""
mock_smile_adam.set_switch_state.side_effect = PlugwiseException
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "switch.cv_pomp_relay"},
blocking=True,
)
assert mock_smile_adam.set_switch_state.call_count == 1
mock_smile_adam.set_switch_state.assert_called_with(
"78d1126fc4c743db81b61c20e88342a7", None, "relay", STATE_OFF
)
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.fibaro_hc2_relay"},
blocking=True,
)
assert mock_smile_adam.set_switch_state.call_count == 2
mock_smile_adam.set_switch_state.assert_called_with(
"a28f588dc4a049a483fd03a30361ad3a", None, "relay", STATE_ON
)
@pytest.mark.parametrize("platforms", [(SWITCH_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_stretch_switch_snapshot(
hass: HomeAssistant,
mock_stretch: MagicMock,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
setup_platform: MockConfigEntry,
) -> None:
"""Test Stretch switch snapshot."""
await snapshot_platform(hass, entity_registry, snapshot, setup_platform.entry_id)
async def test_stretch_switch_changes(
hass: HomeAssistant, mock_stretch: MagicMock, init_integration: MockConfigEntry
) -> None:
"""Test changing of power related switch entities."""
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_OFF,
2024-12-10 16:43:08 +01:00
{ATTR_ENTITY_ID: "switch.koelkast_92c4a_relay"},
blocking=True,
)
2022-02-10 09:53:26 +01:00
assert mock_stretch.set_switch_state.call_count == 1
mock_stretch.set_switch_state.assert_called_with(
2024-09-24 18:11:17 +02:00
"e1c884e7dede431dadee09506ec4f859", None, "relay", STATE_OFF
2022-02-10 09:53:26 +01:00
)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TOGGLE,
2024-12-10 16:43:08 +01:00
{ATTR_ENTITY_ID: "switch.droger_52559_relay"},
blocking=True,
)
2022-02-10 09:53:26 +01:00
assert mock_stretch.set_switch_state.call_count == 2
mock_stretch.set_switch_state.assert_called_with(
2024-09-24 18:11:17 +02:00
"cfe95cf3de1948c0b8955125bf754614", None, "relay", STATE_OFF
2022-02-10 09:53:26 +01:00
)
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
2024-12-10 16:43:08 +01:00
{ATTR_ENTITY_ID: "switch.droger_52559_relay"},
blocking=True,
)
2022-02-10 09:53:26 +01:00
assert mock_stretch.set_switch_state.call_count == 3
mock_stretch.set_switch_state.assert_called_with(
2024-09-24 18:11:17 +02:00
"cfe95cf3de1948c0b8955125bf754614", None, "relay", STATE_ON
2022-02-10 09:53:26 +01:00
)
async def test_unique_id_migration_plug_relay(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_smile_adam: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test unique ID migration of -plugs to -relay."""
mock_config_entry.add_to_hass(hass)
# Entry to migrate
entity_registry.async_get_or_create(
SWITCH_DOMAIN,
DOMAIN,
"21f2b542c49845e6bb416884c55778d6-plug",
config_entry=mock_config_entry,
suggested_object_id="playstation_smart_plug",
disabled_by=None,
)
# Entry not needing migration
entity_registry.async_get_or_create(
SWITCH_DOMAIN,
DOMAIN,
"675416a629f343c495449970e2ca37b5-relay",
config_entry=mock_config_entry,
2023-08-08 16:38:37 +02:00
suggested_object_id="ziggo_modem",
disabled_by=None,
)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get("switch.playstation_smart_plug") is not None
2023-08-08 16:38:37 +02:00
assert hass.states.get("switch.ziggo_modem") is not None
entity_entry = entity_registry.async_get("switch.playstation_smart_plug")
assert entity_entry
assert entity_entry.unique_id == "21f2b542c49845e6bb416884c55778d6-relay"
entity_entry = entity_registry.async_get("switch.ziggo_modem")
assert entity_entry
assert entity_entry.unique_id == "675416a629f343c495449970e2ca37b5-relay"