mirror of
https://github.com/home-assistant/core.git
synced 2025-06-29 09:27:08 +00:00

* Deprecate SmartThings switch entity * Apply suggestions from code review Co-authored-by: Robert Resch <robert@resch.dev> * Fix * Revert "Apply suggestions from code review" This reverts commit c6d39d38de1c8b8cc1a95d79a62b6658776375cc. * Revert "Revert "Apply suggestions from code review"" This reverts commit d92411c1560b031eb44679c3f24f3a6835279570. * Fix * Fix --------- Co-authored-by: Robert Resch <robert@resch.dev>
202 lines
5.8 KiB
Python
202 lines
5.8 KiB
Python
"""Test for the SmartThings switch platform."""
|
|
|
|
from unittest.mock import AsyncMock
|
|
|
|
from pysmartthings import Attribute, Capability, Command
|
|
import pytest
|
|
from syrupy import SnapshotAssertion
|
|
|
|
from homeassistant.components import automation, script
|
|
from homeassistant.components.automation import automations_with_entity
|
|
from homeassistant.components.script import scripts_with_entity
|
|
from homeassistant.components.smartthings.const import DOMAIN, MAIN
|
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
|
from homeassistant.const import (
|
|
ATTR_ENTITY_ID,
|
|
SERVICE_TURN_OFF,
|
|
SERVICE_TURN_ON,
|
|
STATE_OFF,
|
|
STATE_ON,
|
|
Platform,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers import entity_registry as er, issue_registry as ir
|
|
from homeassistant.setup import async_setup_component
|
|
|
|
from . import setup_integration, snapshot_smartthings_entities, trigger_update
|
|
|
|
from tests.common import MockConfigEntry
|
|
|
|
|
|
async def test_all_entities(
|
|
hass: HomeAssistant,
|
|
snapshot: SnapshotAssertion,
|
|
devices: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
entity_registry: er.EntityRegistry,
|
|
) -> None:
|
|
"""Test all entities."""
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
snapshot_smartthings_entities(hass, entity_registry, snapshot, Platform.SWITCH)
|
|
|
|
|
|
@pytest.mark.parametrize("device_fixture", ["c2c_arlo_pro_3_switch"])
|
|
@pytest.mark.parametrize(
|
|
("action", "command"),
|
|
[
|
|
(SERVICE_TURN_ON, Command.ON),
|
|
(SERVICE_TURN_OFF, Command.OFF),
|
|
],
|
|
)
|
|
async def test_switch_turn_on_off(
|
|
hass: HomeAssistant,
|
|
devices: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
action: str,
|
|
command: Command,
|
|
) -> None:
|
|
"""Test switch turn on and off command."""
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
await hass.services.async_call(
|
|
SWITCH_DOMAIN,
|
|
action,
|
|
{ATTR_ENTITY_ID: "switch.2nd_floor_hallway"},
|
|
blocking=True,
|
|
)
|
|
devices.execute_device_command.assert_called_once_with(
|
|
"10e06a70-ee7d-4832-85e9-a0a06a7a05bd", Capability.SWITCH, command, MAIN
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("device_fixture", ["da_wm_wd_000001"])
|
|
@pytest.mark.parametrize(
|
|
("action", "argument"),
|
|
[
|
|
(SERVICE_TURN_ON, "on"),
|
|
(SERVICE_TURN_OFF, "off"),
|
|
],
|
|
)
|
|
async def test_command_switch_turn_on_off(
|
|
hass: HomeAssistant,
|
|
devices: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
action: str,
|
|
argument: str,
|
|
) -> None:
|
|
"""Test switch turn on and off command."""
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
await hass.services.async_call(
|
|
SWITCH_DOMAIN,
|
|
action,
|
|
{ATTR_ENTITY_ID: "switch.dryer_wrinkle_prevent"},
|
|
blocking=True,
|
|
)
|
|
devices.execute_device_command.assert_called_once_with(
|
|
"02f7256e-8353-5bdd-547f-bd5b1647e01b",
|
|
Capability.CUSTOM_DRYER_WRINKLE_PREVENT,
|
|
Command.SET_DRYER_WRINKLE_PREVENT,
|
|
MAIN,
|
|
argument,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("device_fixture", ["c2c_arlo_pro_3_switch"])
|
|
async def test_state_update(
|
|
hass: HomeAssistant,
|
|
devices: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
) -> None:
|
|
"""Test state update."""
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
assert hass.states.get("switch.2nd_floor_hallway").state == STATE_ON
|
|
|
|
await trigger_update(
|
|
hass,
|
|
devices,
|
|
"10e06a70-ee7d-4832-85e9-a0a06a7a05bd",
|
|
Capability.SWITCH,
|
|
Attribute.SWITCH,
|
|
"off",
|
|
)
|
|
|
|
assert hass.states.get("switch.2nd_floor_hallway").state == STATE_OFF
|
|
|
|
|
|
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
|
@pytest.mark.parametrize(
|
|
("device_fixture", "entity_id"),
|
|
[
|
|
("da_wm_wm_000001", "switch.washer"),
|
|
("da_wm_wd_000001", "switch.dryer"),
|
|
],
|
|
)
|
|
async def test_create_issue(
|
|
hass: HomeAssistant,
|
|
devices: AsyncMock,
|
|
mock_config_entry: MockConfigEntry,
|
|
issue_registry: ir.IssueRegistry,
|
|
entity_id: str,
|
|
) -> None:
|
|
"""Test we create an issue when an automation or script is using a deprecated entity."""
|
|
issue_id = f"deprecated_switch_{entity_id}"
|
|
|
|
assert await async_setup_component(
|
|
hass,
|
|
automation.DOMAIN,
|
|
{
|
|
automation.DOMAIN: {
|
|
"id": "test",
|
|
"alias": "test",
|
|
"trigger": {"platform": "state", "entity_id": entity_id},
|
|
"action": {
|
|
"action": "automation.turn_on",
|
|
"target": {
|
|
"entity_id": "automation.test",
|
|
},
|
|
},
|
|
}
|
|
},
|
|
)
|
|
assert await async_setup_component(
|
|
hass,
|
|
script.DOMAIN,
|
|
{
|
|
script.DOMAIN: {
|
|
"test": {
|
|
"sequence": [
|
|
{
|
|
"condition": "state",
|
|
"entity_id": entity_id,
|
|
"state": "on",
|
|
},
|
|
],
|
|
}
|
|
}
|
|
},
|
|
)
|
|
|
|
await setup_integration(hass, mock_config_entry)
|
|
|
|
assert automations_with_entity(hass, entity_id)[0] == "automation.test"
|
|
assert scripts_with_entity(hass, entity_id)[0] == "script.test"
|
|
|
|
assert len(issue_registry.issues) == 1
|
|
issue = issue_registry.async_get_issue(DOMAIN, issue_id)
|
|
assert issue is not None
|
|
assert issue.translation_key == "deprecated_switch_appliance"
|
|
assert issue.translation_placeholders == {
|
|
"entity": entity_id,
|
|
"items": "- [test](/config/automation/edit/test)\n- [test](/config/script/edit/test)",
|
|
}
|
|
|
|
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
|
await hass.async_block_till_done()
|
|
|
|
# Assert the issue is no longer present
|
|
assert not issue_registry.async_get_issue(DOMAIN, issue_id)
|
|
assert len(issue_registry.issues) == 0
|