diff --git a/homeassistant/components/smartthings/__init__.py b/homeassistant/components/smartthings/__init__.py index e5351798219..a8d28e0503f 100644 --- a/homeassistant/components/smartthings/__init__.py +++ b/homeassistant/components/smartthings/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections.abc import Callable +import contextlib from dataclasses import dataclass from http import HTTPStatus import logging @@ -12,15 +13,17 @@ from aiohttp import ClientResponseError from pysmartthings import ( Attribute, Capability, + ComponentStatus, Device, DeviceEvent, + Lifecycle, Scene, SmartThings, SmartThingsAuthenticationFailedError, + SmartThingsConnectionError, SmartThingsSinkError, Status, ) -from pysmartthings.models import Lifecycle from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -72,7 +75,7 @@ class FullDevice: """Define an object to hold device data.""" device: Device - status: dict[str, dict[Capability | str, dict[Attribute | str, Status]]] + status: dict[str, ComponentStatus] type SmartThingsConfigEntry = ConfigEntry[SmartThingsData] @@ -124,7 +127,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: SmartThingsConfigEntry) client.refresh_token_function = _refresh_token def _handle_max_connections() -> None: - _LOGGER.debug("We hit the limit of max connections") + _LOGGER.debug( + "We hit the limit of max connections or we could not remove the old one, so retrying" + ) hass.config_entries.async_schedule_reload(entry.entry_id) client.max_connections_reached_callback = _handle_max_connections @@ -147,7 +152,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: SmartThingsConfigEntry) if (old_identifier := entry.data.get(CONF_SUBSCRIPTION_ID)) is not None: _LOGGER.debug("Trying to delete old subscription %s", old_identifier) - await client.delete_subscription(old_identifier) + try: + await client.delete_subscription(old_identifier) + except SmartThingsConnectionError as err: + raise ConfigEntryNotReady("Could not delete old subscription") from err _LOGGER.debug("Trying to create a new subscription") try: @@ -274,7 +282,8 @@ async def async_unload_entry( """Unload a config entry.""" client = entry.runtime_data.client if (subscription_id := entry.data.get(CONF_SUBSCRIPTION_ID)) is not None: - await client.delete_subscription(subscription_id) + with contextlib.suppress(SmartThingsConnectionError): + await client.delete_subscription(subscription_id) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) @@ -355,9 +364,7 @@ KEEP_CAPABILITY_QUIRK: dict[ } -def process_status( - status: dict[str, dict[Capability | str, dict[Attribute | str, Status]]], -) -> dict[str, dict[Capability | str, dict[Attribute | str, Status]]]: +def process_status(status: dict[str, ComponentStatus]) -> dict[str, ComponentStatus]: """Remove disabled capabilities from status.""" if (main_component := status.get(MAIN)) is None: return status diff --git a/homeassistant/components/smartthings/binary_sensor.py b/homeassistant/components/smartthings/binary_sensor.py index 24249345080..ee68db49929 100644 --- a/homeassistant/components/smartthings/binary_sensor.py +++ b/homeassistant/components/smartthings/binary_sensor.py @@ -174,9 +174,7 @@ def get_main_component_category( device: FullDevice, ) -> Category | str: """Get the main component of a device.""" - main = next( - component for component in device.device.components if component.id == MAIN - ) + main = device.device.components[MAIN] return main.user_category or main.manufacturer_category diff --git a/homeassistant/components/smartthings/entity.py b/homeassistant/components/smartthings/entity.py index 12c07bea983..3314d4b868d 100644 --- a/homeassistant/components/smartthings/entity.py +++ b/homeassistant/components/smartthings/entity.py @@ -8,9 +8,9 @@ from pysmartthings import ( Attribute, Capability, Command, + ComponentStatus, DeviceEvent, SmartThings, - Status, ) from homeassistant.helpers.device_registry import DeviceInfo @@ -38,7 +38,7 @@ class SmartThingsEntity(Entity): self.client = client self.capabilities = capabilities self.component = component - self._internal_state: dict[Capability | str, dict[Attribute | str, Status]] = { + self._internal_state: ComponentStatus = { capability: device.status[component][capability] for capability in capabilities if capability in device.status[component] diff --git a/homeassistant/components/smartthings/event.py b/homeassistant/components/smartthings/event.py index e22a32c7726..8b413f04713 100644 --- a/homeassistant/components/smartthings/event.py +++ b/homeassistant/components/smartthings/event.py @@ -22,10 +22,12 @@ async def async_setup_entry( """Add events for a config entry.""" entry_data = entry.runtime_data async_add_entities( - SmartThingsButtonEvent(entry_data.client, device, component) + SmartThingsButtonEvent( + entry_data.client, device, device.device.components[component] + ) for device in entry_data.devices.values() - for component in device.device.components - if Capability.BUTTON in component.capabilities + for component, capabilities in device.status.items() + if Capability.BUTTON in capabilities ) diff --git a/homeassistant/components/smartthings/manifest.json b/homeassistant/components/smartthings/manifest.json index d7133ce7c6d..49de0c79ce7 100644 --- a/homeassistant/components/smartthings/manifest.json +++ b/homeassistant/components/smartthings/manifest.json @@ -30,5 +30,5 @@ "iot_class": "cloud_push", "loggers": ["pysmartthings"], "quality_scale": "bronze", - "requirements": ["pysmartthings==2.7.4"] + "requirements": ["pysmartthings==3.0.0"] } diff --git a/homeassistant/components/smartthings/valve.py b/homeassistant/components/smartthings/valve.py index 3c401c087ec..4279d528f8b 100644 --- a/homeassistant/components/smartthings/valve.py +++ b/homeassistant/components/smartthings/valve.py @@ -47,8 +47,8 @@ class SmartThingsValve(SmartThingsEntity, ValveEntity): """Init the class.""" super().__init__(client, device, {Capability.VALVE}) self._attr_device_class = DEVICE_CLASS_MAP.get( - device.device.components[0].user_category - or device.device.components[0].manufacturer_category + device.device.components[MAIN].user_category + or device.device.components[MAIN].manufacturer_category ) async def async_open_valve(self) -> None: diff --git a/requirements_all.txt b/requirements_all.txt index 9b856492754..7edd663ba89 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2313,7 +2313,7 @@ pysma==0.7.5 pysmappee==0.2.29 # homeassistant.components.smartthings -pysmartthings==2.7.4 +pysmartthings==3.0.0 # homeassistant.components.smarty pysmarty2==0.10.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 94d60c7b1a9..fafa1008c06 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1883,7 +1883,7 @@ pysma==0.7.5 pysmappee==0.2.29 # homeassistant.components.smartthings -pysmartthings==2.7.4 +pysmartthings==3.0.0 # homeassistant.components.smarty pysmarty2==0.10.2 diff --git a/tests/components/smartthings/__init__.py b/tests/components/smartthings/__init__.py index ad09f1a7acf..fce344b57a7 100644 --- a/tests/components/smartthings/__init__.py +++ b/tests/components/smartthings/__init__.py @@ -3,7 +3,7 @@ from typing import Any from unittest.mock import AsyncMock -from pysmartthings.models import Attribute, Capability, DeviceEvent +from pysmartthings import Attribute, Capability, DeviceEvent from syrupy import SnapshotAssertion from homeassistant.components.smartthings.const import MAIN diff --git a/tests/components/smartthings/conftest.py b/tests/components/smartthings/conftest.py index 761b65adc8a..a19c78dcc00 100644 --- a/tests/components/smartthings/conftest.py +++ b/tests/components/smartthings/conftest.py @@ -4,7 +4,7 @@ from collections.abc import Generator import time from unittest.mock import AsyncMock, patch -from pysmartthings.models import ( +from pysmartthings import ( DeviceResponse, DeviceStatus, LocationResponse, diff --git a/tests/components/smartthings/test_init.py b/tests/components/smartthings/test_init.py index c0d0b8b5840..16458007c29 100644 --- a/tests/components/smartthings/test_init.py +++ b/tests/components/smartthings/test_init.py @@ -8,9 +8,10 @@ from pysmartthings import ( Capability, DeviceResponse, DeviceStatus, + Lifecycle, SmartThingsSinkError, + Subscription, ) -from pysmartthings.models import Lifecycle, Subscription import pytest from syrupy import SnapshotAssertion