-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Add routine management to Alexa Devices #166291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joostlek
merged 6 commits into
home-assistant:dev
from
chemelli74:chemelli74-alexa-routines
Apr 29, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f743def
Add routine management to Alexa Devices
chemelli74 0871878
Merge branch 'dev' into chemelli74-alexa-routines
chemelli74 22debd0
missing PARALLEL_UPDATES
chemelli74 f5d3ca4
apply review comments
chemelli74 4de2c75
code cleanup, remote stale routines, improve tests
chemelli74 85128d4
fix id and related tests
chemelli74 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """Support for buttons.""" | ||
|
|
||
| from homeassistant.components.button import ButtonEntity | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity import EntityDescription | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
| from homeassistant.util import slugify | ||
|
|
||
| from .coordinator import AmazonConfigEntry, AmazonDevicesCoordinator | ||
| from .entity import AmazonServiceEntity | ||
|
|
||
| # Coordinator is used to centralize the data updates | ||
| PARALLEL_UPDATES = 0 | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| entry: AmazonConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up button entities for Alexa Devices.""" | ||
| coordinator = entry.runtime_data | ||
|
|
||
| known_routines: set[str] = set() | ||
|
|
||
| def _check_routines() -> None: | ||
| current_routines = set(coordinator.api.routines) | ||
| new_routines = current_routines - known_routines | ||
| if new_routines: | ||
| known_routines.update(new_routines) | ||
| async_add_entities( | ||
| AmazonRoutineButton(coordinator, routine) for routine in new_routines | ||
| ) | ||
|
|
||
| _check_routines() | ||
| entry.async_on_unload(coordinator.async_add_listener(_check_routines)) | ||
|
|
||
|
|
||
| class AmazonRoutineButton(AmazonServiceEntity, ButtonEntity): | ||
| """Button entity for Alexa routine.""" | ||
|
|
||
| _attr_has_entity_name = True | ||
|
|
||
| def __init__(self, coordinator: AmazonDevicesCoordinator, routine: str) -> None: | ||
| """Initialize the routine button entity.""" | ||
| self._coordinator = coordinator | ||
| self._routine = routine | ||
| super().__init__( | ||
| coordinator, | ||
| EntityDescription(key=slugify(routine), name=routine), | ||
| ) | ||
|
|
||
| async def async_press(self) -> None: | ||
| """Handle button press action.""" | ||
| await self._coordinator.api.call_routine(self._routine) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # serializer version: 1 | ||
| # name: test_all_entities[button.fake_email_gmail_com_test_routine-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': list([ | ||
| None, | ||
| ]), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'button', | ||
| 'entity_category': None, | ||
| 'entity_id': 'button.fake_email_gmail_com_test_routine', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'object_id_base': 'Test Routine', | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Test Routine', | ||
| 'platform': 'alexa_devices', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': None, | ||
| 'unique_id': 'amzn1_account_fake_user_id-test_routine', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_all_entities[button.fake_email_gmail_com_test_routine-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'fake_email@gmail.com Test Routine', | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'button.fake_email_gmail_com_test_routine', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'unknown', | ||
| }) | ||
| # --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """Test Alexa Devices button entities.""" | ||
|
|
||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| from freezegun.api import FrozenDateTimeFactory | ||
| import pytest | ||
| from syrupy.assertion import SnapshotAssertion | ||
|
|
||
| from homeassistant.components.alexa_devices.coordinator import SCAN_INTERVAL | ||
| from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS | ||
| from homeassistant.const import ATTR_ENTITY_ID, Platform | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import entity_registry as er | ||
| from homeassistant.util import slugify | ||
|
|
||
| from . import setup_integration | ||
| from .const import TEST_USERNAME | ||
|
|
||
| from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform | ||
|
|
||
|
|
||
| async def test_all_entities( | ||
| hass: HomeAssistant, | ||
| snapshot: SnapshotAssertion, | ||
| mock_amazon_devices_client: AsyncMock, | ||
| mock_config_entry: MockConfigEntry, | ||
| entity_registry: er.EntityRegistry, | ||
| ) -> None: | ||
| """Test all entities.""" | ||
|
|
||
| with patch("homeassistant.components.alexa_devices.PLATFORMS", [Platform.BUTTON]): | ||
| await setup_integration(hass, mock_config_entry) | ||
|
|
||
| await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) | ||
|
|
||
|
|
||
| async def test_pressing_routine_button( | ||
| hass: HomeAssistant, | ||
| mock_amazon_devices_client: AsyncMock, | ||
| mock_config_entry: MockConfigEntry, | ||
| ) -> None: | ||
| """Test routine run button.""" | ||
|
|
||
| await setup_integration(hass, mock_config_entry) | ||
|
|
||
| await hass.services.async_call( | ||
| BUTTON_DOMAIN, | ||
| SERVICE_PRESS, | ||
| {ATTR_ENTITY_ID: f"button.{slugify(TEST_USERNAME)}_test_routine"}, | ||
| blocking=True, | ||
| ) | ||
| mock_amazon_devices_client.call_routine.assert_called_once() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("initial_routine", "updated_routines"), | ||
| [ | ||
| (["Test Routine"], ["Test Routine", "New Routine"]), # Add a routine | ||
| (["Test Routine", "New Routine"], ["Test Routine"]), # Remove a routine | ||
| (["Test Routine"], []), # Remove all routines | ||
| ], | ||
| ) | ||
| async def test_dynamic_entities( | ||
| hass: HomeAssistant, | ||
| freezer: FrozenDateTimeFactory, | ||
| mock_amazon_devices_client: AsyncMock, | ||
| mock_config_entry: MockConfigEntry, | ||
| initial_routine: list[str], | ||
| updated_routines: list[str], | ||
| ) -> None: | ||
| """Test entities are dynamically created and deleted.""" | ||
|
|
||
| mock_amazon_devices_client.routines = initial_routine | ||
|
|
||
| await setup_integration(hass, mock_config_entry) | ||
|
|
||
| # Check initial routine(s) exist | ||
| for routine in initial_routine: | ||
| entity_id = f"button.{slugify(TEST_USERNAME)}_{slugify(routine)}" | ||
| assert hass.states.get(entity_id) is not None | ||
|
|
||
| mock_amazon_devices_client.routines = updated_routines | ||
|
|
||
| freezer.tick(SCAN_INTERVAL) | ||
| async_fire_time_changed(hass) | ||
| await hass.async_block_till_done() | ||
|
|
||
| # After update, check which routines should exist | ||
| for routine in updated_routines: | ||
| entity_id = f"button.{slugify(TEST_USERNAME)}_{slugify(routine)}" | ||
| assert hass.states.get(entity_id) is not None | ||
|
|
||
| # Check routines that were removed no longer exist | ||
| for routine in set(initial_routine) - set(updated_routines): | ||
| entity_id = f"button.{slugify(TEST_USERNAME)}_{slugify(routine)}" | ||
| assert hass.states.get(entity_id) is None |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.