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

486 lines
17 KiB
Python

"""Tests for the Plugwise switch integration."""
from unittest.mock import MagicMock, patch
from plugwise.exceptions import PlugwiseException
import pytest
from pytest_homeassistant.common import MockConfigEntry, snapshot_platform
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.automation import DOMAIN as AUTOMATION_DOMAIN
from homeassistant.components.plugwise.const import DOMAIN
from homeassistant.components.script import DOMAIN as SCRIPT_DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchService
from homeassistant.const import (
EVENT_HOMEASSISTANT_STARTED,
STATE_OFF,
STATE_ON,
TargetKey,
)
from homeassistant.core import CoreState, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er, issue_registry as ir
from homeassistant.setup import async_setup_component
DHW_CM_SWITCH_UNIQUE_ID = "36b937e44ad145bab165fa0fe99d742d-dhw_cm_switch"
DHW_CM_SWITCH_ISSUE_ID = "deprecated_dhw_cm_switch_36b937e44ad145bab165fa0fe99d742d"
@pytest.mark.usefixtures("mock_smile_adam")
@pytest.mark.parametrize("platforms", [(SWITCH_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_adam_switch_snapshot(
hass: HomeAssistant,
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:
"""Test changing of climate related switch entities."""
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TURN_OFF,
{TargetKey.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
)
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TOGGLE,
{TargetKey.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_OFF
)
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TURN_ON,
{TargetKey.ENTITY_ID: "switch.fibaro_hc2_relay"},
blocking=True,
)
assert mock_smile_adam.set_switch_state.call_count == 3
mock_smile_adam.set_switch_state.assert_called_with(
"a28f588dc4a049a483fd03a30361ad3a", None, "relay", STATE_ON
)
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,
SwitchService.TURN_OFF,
{TargetKey.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,
SwitchService.TURN_ON,
{TargetKey.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.usefixtures("mock_stretch")
@pytest.mark.parametrize("platforms", [(SWITCH_DOMAIN,)])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_stretch_switch_snapshot(
hass: HomeAssistant,
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)
@pytest.mark.parametrize("chosen_env", ["anna_p1"], indirect=True)
@pytest.mark.parametrize("cooling_present", [True], indirect=True)
@pytest.mark.usefixtures("mock_smile_anna")
async def test_deprecated_dhw_comfort_switch_warning(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mock_smile_anna: MagicMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test warning when using the deprecated DHW comfort switch."""
mock_config_entry.add_to_hass(hass)
entity_registry.async_get_or_create(
SWITCH_DOMAIN,
DOMAIN,
DHW_CM_SWITCH_UNIQUE_ID,
config_entry=mock_config_entry,
suggested_object_id="opentherm_dhw_cm_switch",
)
with patch("homeassistant.components.plugwise.PLATFORMS", [SWITCH_DOMAIN]):
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
entity_id = entity_registry.async_get_entity_id(
SWITCH_DOMAIN, DOMAIN, DHW_CM_SWITCH_UNIQUE_ID
)
assert entity_id
issue = issue_registry.async_get_issue(DOMAIN, DHW_CM_SWITCH_ISSUE_ID)
assert issue is not None
assert issue.translation_key == "deprecated_dhw_cm_switch"
assert issue.config_entry_id == mock_config_entry.entry_id
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TURN_ON,
{TargetKey.ENTITY_ID: entity_id},
blocking=True,
)
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TURN_OFF,
{TargetKey.ENTITY_ID: entity_id},
blocking=True,
)
assert not any(
"incorrectly returns a coroutine object" in record.message
for record in caplog.records
)
assert mock_smile_anna.set_switch_state.call_count == 2
mock_smile_anna.set_switch_state.assert_any_call(
"36b937e44ad145bab165fa0fe99d742d", None, "dhw_cm_switch", STATE_ON
)
mock_smile_anna.set_switch_state.assert_any_call(
"36b937e44ad145bab165fa0fe99d742d", None, "dhw_cm_switch", STATE_OFF
)
await hass.config_entries.async_remove(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert issue_registry.async_get_issue(DOMAIN, DHW_CM_SWITCH_ISSUE_ID) is None
@pytest.mark.parametrize("chosen_env", ["anna_p1"], indirect=True)
@pytest.mark.parametrize("cooling_present", [True], indirect=True)
@pytest.mark.usefixtures("mock_smile_anna")
async def test_deprecated_dhw_comfort_switch_not_created_for_new_install(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test the deprecated DHW comfort switch is not added on a fresh install."""
mock_config_entry.add_to_hass(hass)
with patch("homeassistant.components.plugwise.PLATFORMS", [SWITCH_DOMAIN]):
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert (
entity_registry.async_get_entity_id(
SWITCH_DOMAIN, DOMAIN, DHW_CM_SWITCH_UNIQUE_ID
)
is None
)
assert (DOMAIN, DHW_CM_SWITCH_ISSUE_ID) not in issue_registry.issues
@pytest.mark.parametrize("chosen_env", ["anna_p1"], indirect=True)
@pytest.mark.parametrize("cooling_present", [True], indirect=True)
@pytest.mark.usefixtures("mock_smile_anna")
async def test_deprecated_dhw_comfort_switch_removed_when_disabled(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test a disabled, unused DHW comfort switch is removed."""
mock_config_entry.add_to_hass(hass)
entity_registry.async_get_or_create(
SWITCH_DOMAIN,
DOMAIN,
DHW_CM_SWITCH_UNIQUE_ID,
config_entry=mock_config_entry,
suggested_object_id="opentherm_dhw_cm_switch",
disabled_by=er.RegistryEntryDisabler.USER,
)
with patch("homeassistant.components.plugwise.PLATFORMS", [SWITCH_DOMAIN]):
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert (
entity_registry.async_get_entity_id(
SWITCH_DOMAIN, DOMAIN, DHW_CM_SWITCH_UNIQUE_ID
)
is None
)
assert (DOMAIN, DHW_CM_SWITCH_ISSUE_ID) not in issue_registry.issues
@pytest.mark.parametrize("chosen_env", ["anna_p1"], indirect=True)
@pytest.mark.parametrize("cooling_present", [True], indirect=True)
@pytest.mark.usefixtures("mock_smile_anna")
async def test_deferred_dhw_switch_check_unsubscribed_when_entry_unloads(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test deferred entity checks are cancelled when the config entry unloads."""
mock_config_entry.add_to_hass(hass)
entity_registry.async_get_or_create(
SWITCH_DOMAIN,
DOMAIN,
DHW_CM_SWITCH_UNIQUE_ID,
config_entry=mock_config_entry,
suggested_object_id="opentherm_dhw_cm_switch",
disabled_by=er.RegistryEntryDisabler.USER,
)
hass.set_state(CoreState.starting)
with patch("homeassistant.components.plugwise.PLATFORMS", [SWITCH_DOMAIN]):
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
hass.set_state(CoreState.running)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
assert entity_registry.async_get_entity_id(
SWITCH_DOMAIN, DOMAIN, DHW_CM_SWITCH_UNIQUE_ID
)
@pytest.mark.parametrize("chosen_env", ["anna_p1"], indirect=True)
@pytest.mark.parametrize("cooling_present", [True], indirect=True)
@pytest.mark.usefixtures("mock_smile_anna")
async def test_deferred_dhw_switch_check_on_homeassistant_started(
hass: HomeAssistant,
caplog: pytest.LogCaptureFixture,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test the deferred entity check runs once Home Assistant has started."""
mock_config_entry.add_to_hass(hass)
entity_registry.async_get_or_create(
SWITCH_DOMAIN,
DOMAIN,
DHW_CM_SWITCH_UNIQUE_ID,
config_entry=mock_config_entry,
suggested_object_id="opentherm_dhw_cm_switch",
disabled_by=er.RegistryEntryDisabler.USER,
)
hass.set_state(CoreState.starting)
with patch("homeassistant.components.plugwise.PLATFORMS", [SWITCH_DOMAIN]):
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert entity_registry.async_get_entity_id(
SWITCH_DOMAIN, DOMAIN, DHW_CM_SWITCH_UNIQUE_ID
)
hass.set_state(CoreState.running)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
assert (
entity_registry.async_get_entity_id(
SWITCH_DOMAIN, DOMAIN, DHW_CM_SWITCH_UNIQUE_ID
)
is None
)
assert await hass.config_entries.async_unload(mock_config_entry.entry_id)
assert "Unable to remove unknown job listener" not in caplog.text
@pytest.mark.parametrize("chosen_env", ["anna_p1"], indirect=True)
@pytest.mark.parametrize("cooling_present", [True], indirect=True)
@pytest.mark.usefixtures("mock_smile_anna")
async def test_deprecated_dhw_comfort_switch_kept_when_referenced(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
issue_registry: ir.IssueRegistry,
) -> None:
"""Test a disabled DHW comfort switch referenced by an automation and script is kept."""
mock_config_entry.add_to_hass(hass)
entity_registry.async_get_or_create(
SWITCH_DOMAIN,
DOMAIN,
DHW_CM_SWITCH_UNIQUE_ID,
config_entry=mock_config_entry,
suggested_object_id="opentherm_dhw_cm_switch",
disabled_by=er.RegistryEntryDisabler.USER,
)
hass.set_state(CoreState.starting)
with patch("homeassistant.components.plugwise.PLATFORMS", [SWITCH_DOMAIN]):
assert await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert entity_registry.async_get_entity_id(
SWITCH_DOMAIN, DOMAIN, DHW_CM_SWITCH_UNIQUE_ID
)
assert (DOMAIN, DHW_CM_SWITCH_ISSUE_ID) not in issue_registry.issues
assert await async_setup_component(
hass,
AUTOMATION_DOMAIN,
{
AUTOMATION_DOMAIN: {
"alias": "Test automation",
"trigger": {
"platform": "state",
"entity_id": "switch.opentherm_dhw_cm_switch",
},
"action": [],
}
},
)
assert await async_setup_component(
hass,
SCRIPT_DOMAIN,
{
SCRIPT_DOMAIN: {
"test_script": {
"sequence": {
"action": "switch.turn_on",
"target": {"entity_id": "switch.opentherm_dhw_cm_switch"},
}
}
}
},
)
hass.set_state(CoreState.running)
hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
await hass.async_block_till_done()
assert (
entity_registry.async_get_entity_id(
SWITCH_DOMAIN, DOMAIN, DHW_CM_SWITCH_UNIQUE_ID
)
is not None
)
issue = issue_registry.async_get_issue(DOMAIN, DHW_CM_SWITCH_ISSUE_ID)
assert issue is not None
assert issue.translation_key == "deprecated_dhw_cm_switch_scripts"
assert issue.config_entry_id == mock_config_entry.entry_id
assert issue.translation_placeholders is not None
assert "automation.test_automation" in issue.translation_placeholders["items"]
assert "/config/script/edit/test_script" in issue.translation_placeholders["items"]
await hass.config_entries.async_remove(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert issue_registry.async_get_issue(DOMAIN, DHW_CM_SWITCH_ISSUE_ID) is None
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,
SwitchService.TURN_OFF,
{TargetKey.ENTITY_ID: "switch.koelkast_92c4a_relay"},
blocking=True,
)
assert mock_stretch.set_switch_state.call_count == 1
mock_stretch.set_switch_state.assert_called_with(
"e1c884e7dede431dadee09506ec4f859", None, "relay", STATE_OFF
)
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TOGGLE,
{TargetKey.ENTITY_ID: "switch.droger_52559_relay"},
blocking=True,
)
assert mock_stretch.set_switch_state.call_count == 2
mock_stretch.set_switch_state.assert_called_with(
"cfe95cf3de1948c0b8955125bf754614", None, "relay", STATE_OFF
)
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TURN_ON,
{TargetKey.ENTITY_ID: "switch.droger_52559_relay"},
blocking=True,
)
assert mock_stretch.set_switch_state.call_count == 3
mock_stretch.set_switch_state.assert_called_with(
"cfe95cf3de1948c0b8955125bf754614", None, "relay", STATE_ON
)
@pytest.mark.usefixtures("mock_smile_adam")
async def test_unique_id_migration_plug_relay(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
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,
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
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"