diff --git a/custom_components/alexa_media/alarm_control_panel.py b/custom_components/alexa_media/alarm_control_panel.py index fe853eca..a8b5f845 100644 --- a/custom_components/alexa_media/alarm_control_panel.py +++ b/custom_components/alexa_media/alarm_control_panel.py @@ -14,12 +14,12 @@ from homeassistant.components.alarm_control_panel import AlarmControlPanel from homeassistant.const import (STATE_ALARM_ARMED_AWAY, STATE_ALARM_DISARMED) -from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.event import async_call_later from . import DATA_ALEXAMEDIA from . import DOMAIN as ALEXA_DOMAIN from . import MIN_TIME_BETWEEN_FORCED_SCANS, MIN_TIME_BETWEEN_SCANS, hide_email +from .helpers import add_devices _LOGGER = logging.getLogger(__name__) @@ -48,20 +48,7 @@ async def async_setup_platform(hass, [account] ['entities'] ['alarm_control_panel']) = alexa_client - if devices: - _LOGGER.debug("Adding %s", devices) - try: - add_devices_callback(devices, True) - except HomeAssistantError as exception_: - message = exception_.message # type: str - if message.startswith("Entity id already exists"): - _LOGGER.debug("Device already added: %s", - message) - else: - _LOGGER.debug("Unable to add devices: %s : %s", - devices, - message) - return True + return await add_devices(devices, add_devices_callback) class AlexaAlarmControlPanel(AlarmControlPanel): diff --git a/custom_components/alexa_media/helpers.py b/custom_components/alexa_media/helpers.py new file mode 100644 index 00000000..c27b67b7 --- /dev/null +++ b/custom_components/alexa_media/helpers.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: Apache-2.0 +""" +Helper functions for Alexa Media Player. + +For more details about this platform, please refer to the documentation at +https://community.home-assistant.io/t/echo-devices-alexa-as-media-player-testers-needed/58639 +""" + +import logging +from typing import List +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.entity_component import EntityComponent + +_LOGGER = logging.getLogger(__name__) + + +async def add_devices(devices: List[EntityComponent], + add_devices_callback: callable) -> bool: + """Add devices using add_devices_callback.""" + _LOGGER.debug("Adding %s", devices) + if devices: + try: + await add_devices_callback(devices, True) + return True + except HomeAssistantError as exception_: + message = exception_.message # type: str + if message.startswith("Entity id already exists"): + _LOGGER.debug("Device already added: %s", + message) + else: + _LOGGER.debug("Unable to add devices: %s : %s", + devices, + message) + return False diff --git a/custom_components/alexa_media/media_player.py b/custom_components/alexa_media/media_player.py index 88e88273..674e6759 100644 --- a/custom_components/alexa_media/media_player.py +++ b/custom_components/alexa_media/media_player.py @@ -30,13 +30,13 @@ SUPPORT_VOLUME_SET) from homeassistant.const import (STATE_IDLE, STATE_PAUSED, STATE_PLAYING, STATE_STANDBY) -from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_validation as cv from homeassistant.helpers.event import async_call_later from homeassistant.helpers.service import extract_entity_ids from homeassistant.helpers.discovery import async_load_platform from .const import ATTR_MESSAGE, PLAY_SCAN_INTERVAL +from .helpers import add_devices from . import ( DOMAIN as ALEXA_DOMAIN, @@ -73,18 +73,7 @@ async def async_setup_platform(hass, config, add_devices_callback, [account] ['entities'] ['media_player'][key]) = alexa_client - _LOGGER.debug("Adding %s", devices) - try: - add_devices_callback(devices, True) - except HomeAssistantError as exception_: - message = exception_.message # type: str - if message.startswith("Entity id already exists"): - _LOGGER.debug("Device already added: %s", - message) - else: - _LOGGER.debug("Unable to add devices: %s : %s", - devices, - message) + return await add_devices(devices, add_devices_callback) class AlexaClient(MediaPlayerDevice): diff --git a/custom_components/alexa_media/switch.py b/custom_components/alexa_media/switch.py index 4f531531..48034c0e 100644 --- a/custom_components/alexa_media/switch.py +++ b/custom_components/alexa_media/switch.py @@ -21,6 +21,7 @@ MIN_TIME_BETWEEN_FORCED_SCANS, MIN_TIME_BETWEEN_SCANS, hide_email, hide_serial ) +from .helpers import add_devices _LOGGER = logging.getLogger(__name__) @@ -75,9 +76,7 @@ async def async_setup_platform(hass, config, add_devices_callback, switch_key, alexa_client.is_on) devices.append(alexa_client) - if devices: - add_devices_callback(devices, True) - return True + return await add_devices(devices, add_devices_callback) class AlexaMediaSwitch(SwitchDevice):