mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 19:09:32 +00:00
Delete subscription on shutdown of SmartThings (#140135)
* Cache subscription url in SmartThings * Cache subscription url in SmartThings * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Fix * Bump pysmartthings to 2.7.1 * 2.7.2 --------- Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
committed by
GitHub
parent
3c57b12cd1
commit
71159c755f
@@ -2,12 +2,21 @@
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from pysmartthings import Attribute, Capability, DeviceResponse, DeviceStatus
|
||||
from pysmartthings import (
|
||||
Attribute,
|
||||
Capability,
|
||||
DeviceResponse,
|
||||
DeviceStatus,
|
||||
SmartThingsSinkError,
|
||||
)
|
||||
from pysmartthings.models import Subscription
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
|
||||
from homeassistant.components.smartthings import EVENT_BUTTON
|
||||
from homeassistant.components.smartthings.const import DOMAIN
|
||||
from homeassistant.components.smartthings.const import CONF_SUBSCRIPTION_ID, DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.core import Event, HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
@@ -63,6 +72,178 @@ async def test_button_event(
|
||||
assert events[0] == snapshot
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_create_subscription(
|
||||
hass: HomeAssistant,
|
||||
devices: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test creating a subscription."""
|
||||
assert CONF_SUBSCRIPTION_ID not in mock_config_entry.data
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
devices.create_subscription.assert_called_once()
|
||||
|
||||
assert (
|
||||
mock_config_entry.data[CONF_SUBSCRIPTION_ID]
|
||||
== "f5768ce8-c9e5-4507-9020-912c0c60e0ab"
|
||||
)
|
||||
|
||||
devices.subscribe.assert_called_once_with(
|
||||
"397678e5-9995-4a39-9d9f-ae6ba310236c",
|
||||
"5aaaa925-2be1-4e40-b257-e4ef59083324",
|
||||
Subscription.from_json(load_fixture("subscription.json", DOMAIN)),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_create_subscription_sink_error(
|
||||
hass: HomeAssistant,
|
||||
devices: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
snapshot: SnapshotAssertion,
|
||||
) -> None:
|
||||
"""Test handling an error when creating a subscription."""
|
||||
assert CONF_SUBSCRIPTION_ID not in mock_config_entry.data
|
||||
|
||||
devices.create_subscription.side_effect = SmartThingsSinkError("Sink error")
|
||||
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
devices.subscribe.assert_not_called()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
assert CONF_SUBSCRIPTION_ID not in mock_config_entry.data
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_update_subscription_identifier(
|
||||
hass: HomeAssistant,
|
||||
devices: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test updating the subscription identifier."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert (
|
||||
mock_config_entry.data[CONF_SUBSCRIPTION_ID]
|
||||
== "f5768ce8-c9e5-4507-9020-912c0c60e0ab"
|
||||
)
|
||||
|
||||
devices.new_subscription_id_callback("abc")
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.data[CONF_SUBSCRIPTION_ID] == "abc"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_stale_subscription_id(
|
||||
hass: HomeAssistant,
|
||||
devices: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test updating the subscription identifier."""
|
||||
mock_config_entry.add_to_hass(hass)
|
||||
|
||||
hass.config_entries.async_update_entry(
|
||||
mock_config_entry,
|
||||
data={**mock_config_entry.data, CONF_SUBSCRIPTION_ID: "test"},
|
||||
)
|
||||
|
||||
await hass.config_entries.async_setup(mock_config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
mock_config_entry.data[CONF_SUBSCRIPTION_ID]
|
||||
== "f5768ce8-c9e5-4507-9020-912c0c60e0ab"
|
||||
)
|
||||
devices.delete_subscription.assert_called_once_with("test")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_remove_subscription_identifier(
|
||||
hass: HomeAssistant,
|
||||
devices: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test removing the subscription identifier."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert (
|
||||
mock_config_entry.data[CONF_SUBSCRIPTION_ID]
|
||||
== "f5768ce8-c9e5-4507-9020-912c0c60e0ab"
|
||||
)
|
||||
|
||||
devices.new_subscription_id_callback(None)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.data[CONF_SUBSCRIPTION_ID] is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_max_connections_handling(
|
||||
hass: HomeAssistant, devices: AsyncMock, mock_config_entry: MockConfigEntry
|
||||
) -> None:
|
||||
"""Test handling reaching max connections."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert (
|
||||
mock_config_entry.data[CONF_SUBSCRIPTION_ID]
|
||||
== "f5768ce8-c9e5-4507-9020-912c0c60e0ab"
|
||||
)
|
||||
|
||||
devices.create_subscription.side_effect = SmartThingsSinkError("Sink error")
|
||||
|
||||
devices.max_connections_reached_callback()
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_unloading(
|
||||
hass: HomeAssistant,
|
||||
devices: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test unloading the integration."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
await hass.config_entries.async_unload(mock_config_entry.entry_id)
|
||||
devices.delete_subscription.assert_called_once_with(
|
||||
"f5768ce8-c9e5-4507-9020-912c0c60e0ab"
|
||||
)
|
||||
# Deleting the subscription automatically deletes the subscription ID
|
||||
devices.new_subscription_id_callback(None)
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert mock_config_entry.data[CONF_SUBSCRIPTION_ID] is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_shutdown(
|
||||
hass: HomeAssistant,
|
||||
devices: AsyncMock,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Test shutting down Home Assistant."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
|
||||
devices.delete_subscription.assert_called_once_with(
|
||||
"f5768ce8-c9e5-4507-9020-912c0c60e0ab"
|
||||
)
|
||||
# Deleting the subscription automatically deletes the subscription ID
|
||||
devices.new_subscription_id_callback(None)
|
||||
|
||||
assert mock_config_entry.state is ConfigEntryState.LOADED
|
||||
assert mock_config_entry.data[CONF_SUBSCRIPTION_ID] is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize("device_fixture", ["da_ac_rac_000001"])
|
||||
async def test_removing_stale_devices(
|
||||
hass: HomeAssistant,
|
||||
|
||||
Reference in New Issue
Block a user