From b036a787767c4f8cdefd700b00dc05573b5cab11 Mon Sep 17 00:00:00 2001 From: jameson_uk <1040621+jamesonuk@users.noreply.github.com> Date: Tue, 3 Feb 2026 10:48:57 +0000 Subject: [PATCH] Remove invalid notification sensors for Alexa devices (#160422) Co-authored-by: Simone Chemelli --- .../components/alexa_devices/manifest.json | 2 +- .../components/alexa_devices/sensor.py | 5 +++ .../components/alexa_devices/utils.py | 30 ++++++++++++++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/alexa_devices/const.py | 2 + .../snapshots/test_services.ambr | 2 + tests/components/alexa_devices/test_utils.py | 40 +++++++++++++++++++ 8 files changed, 82 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/alexa_devices/manifest.json b/homeassistant/components/alexa_devices/manifest.json index 82a232fa7cf3..ff81c650eaf0 100644 --- a/homeassistant/components/alexa_devices/manifest.json +++ b/homeassistant/components/alexa_devices/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "loggers": ["aioamazondevices"], "quality_scale": "platinum", - "requirements": ["aioamazondevices==11.0.2"] + "requirements": ["aioamazondevices==11.1.1"] } diff --git a/homeassistant/components/alexa_devices/sensor.py b/homeassistant/components/alexa_devices/sensor.py index 383763f0614a..e71c689be280 100644 --- a/homeassistant/components/alexa_devices/sensor.py +++ b/homeassistant/components/alexa_devices/sensor.py @@ -28,6 +28,7 @@ from homeassistant.helpers.typing import StateType from .const import CATEGORY_NOTIFICATIONS, CATEGORY_SENSORS from .coordinator import AmazonConfigEntry from .entity import AmazonEntity +from .utils import async_remove_unsupported_notification_sensors # Coordinator is used to centralize the data updates PARALLEL_UPDATES = 0 @@ -105,6 +106,9 @@ async def async_setup_entry( coordinator = entry.runtime_data + # Remove notification sensors from unsupported devices + await async_remove_unsupported_notification_sensors(hass, coordinator) + known_devices: set[str] = set() def _check_device() -> None: @@ -122,6 +126,7 @@ async def async_setup_entry( AmazonSensorEntity(coordinator, serial_num, notification_desc) for notification_desc in NOTIFICATIONS for serial_num in new_devices + if coordinator.data[serial_num].notifications_supported ] async_add_entities(sensors_list + notifications_list) diff --git a/homeassistant/components/alexa_devices/utils.py b/homeassistant/components/alexa_devices/utils.py index d86f1c9e7600..72a682fa5587 100644 --- a/homeassistant/components/alexa_devices/utils.py +++ b/homeassistant/components/alexa_devices/utils.py @@ -5,8 +5,14 @@ from functools import wraps from typing import Any, Concatenate from aioamazondevices.const.devices import SPEAKER_GROUP_FAMILY +from aioamazondevices.const.schedules import ( + NOTIFICATION_ALARM, + NOTIFICATION_REMINDER, + NOTIFICATION_TIMER, +) from aioamazondevices.exceptions import CannotConnect, CannotRetrieveData +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -81,3 +87,27 @@ async def async_remove_dnd_from_virtual_group( if entity_id and is_group: entity_registry.async_remove(entity_id) _LOGGER.debug("Removed DND switch from virtual group %s", entity_id) + + +async def async_remove_unsupported_notification_sensors( + hass: HomeAssistant, + coordinator: AmazonDevicesCoordinator, +) -> None: + """Remove notification sensors from unsupported devices.""" + entity_registry = er.async_get(hass) + + for serial_num in coordinator.data: + for notification_key in ( + NOTIFICATION_ALARM, + NOTIFICATION_REMINDER, + NOTIFICATION_TIMER, + ): + unique_id = f"{serial_num}-{notification_key}" + entity_id = entity_registry.async_get_entity_id( + domain=SENSOR_DOMAIN, platform=DOMAIN, unique_id=unique_id + ) + is_unsupported = not coordinator.data[serial_num].notifications_supported + + if entity_id and is_unsupported: + entity_registry.async_remove(entity_id) + _LOGGER.debug("Removed unsupported notification sensor %s", entity_id) diff --git a/requirements_all.txt b/requirements_all.txt index 521a703be50b..d41ea8a84a8f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -190,7 +190,7 @@ aioairzone-cloud==0.7.2 aioairzone==1.0.5 # homeassistant.components.alexa_devices -aioamazondevices==11.0.2 +aioamazondevices==11.1.1 # homeassistant.components.ambient_network # homeassistant.components.ambient_station diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 44c40ebb9ddc..967a496eb700 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -181,7 +181,7 @@ aioairzone-cloud==0.7.2 aioairzone==1.0.5 # homeassistant.components.alexa_devices -aioamazondevices==11.0.2 +aioamazondevices==11.1.1 # homeassistant.components.ambient_network # homeassistant.components.ambient_station diff --git a/tests/components/alexa_devices/const.py b/tests/components/alexa_devices/const.py index a36136bdead8..45a35beda7c3 100644 --- a/tests/components/alexa_devices/const.py +++ b/tests/components/alexa_devices/const.py @@ -46,6 +46,7 @@ TEST_DEVICE_1 = AmazonDevice( scale="CELSIUS", ), }, + notifications_supported=True, notifications={ NOTIFICATION_ALARM: AmazonSchedule( type=NOTIFICATION_ALARM, @@ -93,5 +94,6 @@ TEST_DEVICE_2 = AmazonDevice( scale="CELSIUS", ) }, + notifications_supported=False, notifications={}, ) diff --git a/tests/components/alexa_devices/snapshots/test_services.ambr b/tests/components/alexa_devices/snapshots/test_services.ambr index e0b3f11f2f43..c4bcd0fcefc3 100644 --- a/tests/components/alexa_devices/snapshots/test_services.ambr +++ b/tests/components/alexa_devices/snapshots/test_services.ambr @@ -37,6 +37,7 @@ 'type': 'Timer', }), }), + 'notifications_supported': True, 'online': True, 'sensors': dict({ 'dnd': dict({ @@ -103,6 +104,7 @@ 'type': 'Timer', }), }), + 'notifications_supported': True, 'online': True, 'sensors': dict({ 'dnd': dict({ diff --git a/tests/components/alexa_devices/test_utils.py b/tests/components/alexa_devices/test_utils.py index 38c6d340a3ff..5aec72eb2c78 100644 --- a/tests/components/alexa_devices/test_utils.py +++ b/tests/components/alexa_devices/test_utils.py @@ -7,6 +7,7 @@ from aioamazondevices.exceptions import CannotConnect, CannotRetrieveData import pytest from homeassistant.components.alexa_devices.const import DOMAIN +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SERVICE_TURN_ON from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF from homeassistant.core import HomeAssistant @@ -134,3 +135,42 @@ async def test_alexa_dnd_group_removal( await hass.async_block_till_done() assert not hass.states.get(entity.entity_id) + + +async def test_alexa_unsupported_notification_sensor_removal( + hass: HomeAssistant, + mock_amazon_devices_client: AsyncMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, + entity_registry: er.EntityRegistry, +) -> None: + """Test notification sensors are removed from devices that do not support them.""" + + mock_config_entry.add_to_hass(hass) + + device = device_registry.async_get_or_create( + config_entry_id=mock_config_entry.entry_id, + identifiers={(DOMAIN, mock_config_entry.entry_id)}, + name=mock_config_entry.title, + manufacturer="Amazon", + model=SPEAKER_GROUP_MODEL, + entry_type=dr.DeviceEntryType.SERVICE, + ) + + entity = entity_registry.async_get_or_create( + DOMAIN, + SENSOR_DOMAIN, + unique_id=f"{TEST_DEVICE_1_SN}-Timer", + device_id=device.id, + config_entry=mock_config_entry, + has_entity_name=True, + ) + + mock_amazon_devices_client.get_devices_data.return_value[ + TEST_DEVICE_1_SN + ].notifications_supported = False + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert not hass.states.get(entity.entity_id)