$ show alandtse/alexa_media_player
This repository is a Home Assistant custom component that integrates Amazon Alexa/Echo devices into Home Assistant. It allows users to control media playback (play, pause, volume, source selection), manage alarms and timers, control smart home devices (lights, switches, sensors, alarm control panel for Alexa Guard), and send notifications (TTS, announcements, mobile push). The component authenticates with Amazon's unofficial internal API via the `alexapy` library, supporting OAuth, TOTP, and 2FA. Architecture follows Home Assistant's component pattern: a config flow for setup, multiple entity platforms (media_player, sensor, switch, light, alarm_control_panel, binary_sensor, notify), a `DataUpdateCoordinator` for polling with HTTP2 push fallback, and custom services. Data flows from Amazon's cloud to HA via either polling (configurable interval) or push updates (WebSocket/HTTP2), with caching and debouncing to reduce API calls. External dependencies include `alexapy`, `packaging`, `wrapt`, and `dictor`. The component is distributed via HACS (Home Assistant Community Store) and supports multiple Amazon accounts with extensive localization.
The LLM identified 2 files worth highlighting for reuse:
Provides reusable utility functions for the integration: `_catch_login_errors` decorator that catches `AlexapyLoginError` and triggers reauthentication, `retry_async` decorator for retrying async functions with exponential backoff, `safe_get` for nested dictionary access with type checking and dot-escape, `add_devices` for normalized entity filtering, and `alarm_just_dismissed` heuristic. These patterns are generic and could be extracted for any HA integration that requires robust error handling, retries, or safe data access.
Implements performance tracking and caching: `BootMetrics` records startup timing stages, `DataCache` provides a TTL-based cache (max 128 entries, configurable timeout) for reducing redundant API calls, and `AlexaMetrics` aggregates API call statistics (total, cached, missed) and supports debug logging. The `DataCache` pattern is a clean, reusable caching solution for any data-fetching component that needs to avoid repeated requests without external dependencies.