Skip to content

Commit

Permalink
fix: add catch for HomeAssistantError when adding entities
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Sep 2, 2019
1 parent e102848 commit 43acc8b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
17 changes: 2 additions & 15 deletions custom_components/alexa_media/alarm_control_panel.py
Expand Up @@ -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__)

Expand Down Expand Up @@ -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):
Expand Down
36 changes: 36 additions & 0 deletions 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
15 changes: 2 additions & 13 deletions custom_components/alexa_media/media_player.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions custom_components/alexa_media/switch.py
Expand Up @@ -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__)

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 43acc8b

Please sign in to comment.