Skip to content

Commit

Permalink
feat: allow exclusions for alarm_control_panel and switches
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Sep 3, 2019
1 parent bcc0de7 commit 169545f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
9 changes: 7 additions & 2 deletions custom_components/alexa_media/alarm_control_panel.py
Expand Up @@ -21,7 +21,8 @@
from . import (
CONF_EMAIL,
MIN_TIME_BETWEEN_FORCED_SCANS,
MIN_TIME_BETWEEN_SCANS, hide_email
MIN_TIME_BETWEEN_SCANS, hide_email,
CONF_EXCLUDE_DEVICES, CONF_INCLUDE_DEVICES
)
from .helpers import add_devices

Expand All @@ -38,6 +39,8 @@ async def async_setup_platform(hass,
devices = [] # type: List[AlexaAlarmControlPanel]
config = discovery_info['config']
account = config[CONF_EMAIL]
include_filter = config.get(CONF_INCLUDE_DEVICES, [])
exclude_filter = config.get(CONF_EXCLUDE_DEVICES, [])
account_dict = hass.data[DATA_ALEXAMEDIA]['accounts'][account]
if 'alarm_control_panel' not in (account_dict
['entities']):
Expand Down Expand Up @@ -65,7 +68,9 @@ async def async_setup_platform(hass,
_LOGGER.debug("%s: Skipping already added device: %s",
hide_email(account),
alexa_client)
return await add_devices(devices, add_devices_callback)
return await add_devices(hide_email(account),
devices, add_devices_callback,
include_filter, exclude_filter)


class AlexaAlarmControlPanel(AlarmControlPanel):
Expand Down
29 changes: 22 additions & 7 deletions custom_components/alexa_media/helpers.py
Expand Up @@ -9,35 +9,50 @@
"""

import logging
from typing import List
from typing import List, Text
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:
async def add_devices(account: Text, devices: List[EntityComponent],
add_devices_callback: callable,
include_filter: List[Text] = [],
exclude_filter: List[Text] = []) -> bool:
"""Add devices using add_devices_callback."""
new_devices = []
for device in devices:
if (include_filter and device.name not in include_filter
or exclude_filter and device.name in exclude_filter):
_LOGGER.debug("%s: Excluding device: %s",
account,
device)
continue
new_devices.append(device)
devices = new_devices
if devices:
_LOGGER.debug("Adding %s", devices)
_LOGGER.debug("%s: Adding %s", account, devices)
try:
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",
_LOGGER.debug("%s: Device already added: %s",
account,
message)
else:
_LOGGER.debug("Unable to add devices: %s : %s",
_LOGGER.debug("%s: Unable to add devices: %s : %s",
account,
devices,
message)
except BaseException as ex:
template = ("An exception of type {0} occurred."
" Arguments:\n{1!r}")
message = template.format(type(ex).__name__, ex.args)
_LOGGER.debug("Unable to add devices: %s",
_LOGGER.debug("%s: Unable to add devices: %s",
account,
message)

return False
4 changes: 3 additions & 1 deletion custom_components/alexa_media/media_player.py
Expand Up @@ -80,7 +80,9 @@ async def async_setup_platform(hass, config, add_devices_callback,
hide_serial(key),
alexa_client
)
return await add_devices(devices, add_devices_callback)
return await add_devices(hide_email(account),
devices,
add_devices_callback)


class AlexaClient(MediaPlayerDevice):
Expand Down
10 changes: 7 additions & 3 deletions custom_components/alexa_media/switch.py
Expand Up @@ -19,7 +19,8 @@
from . import DOMAIN as ALEXA_DOMAIN
from . import (
MIN_TIME_BETWEEN_FORCED_SCANS, MIN_TIME_BETWEEN_SCANS,
hide_email, hide_serial, CONF_EMAIL
hide_email, hide_serial, CONF_EMAIL,
CONF_EXCLUDE_DEVICES, CONF_INCLUDE_DEVICES
)
from .helpers import add_devices

Expand All @@ -38,6 +39,8 @@ async def async_setup_platform(hass, config, add_devices_callback,
]
config = discovery_info['config']
account = config[CONF_EMAIL]
include_filter = config.get(CONF_INCLUDE_DEVICES, [])
exclude_filter = config.get(CONF_EXCLUDE_DEVICES, [])
account_dict = hass.data[DATA_ALEXAMEDIA]['accounts'][account]
if 'switch' not in account_dict['entities']:
(hass.data[DATA_ALEXAMEDIA]
Expand Down Expand Up @@ -88,8 +91,9 @@ async def async_setup_platform(hass, config, add_devices_callback,
hide_email(account),
key,
alexa_client)
return await add_devices(devices, add_devices_callback)

return await add_devices(hide_email(account),
devices, add_devices_callback,
include_filter, exclude_filter)

class AlexaMediaSwitch(SwitchDevice):
"""Representation of a Alexa Media switch."""
Expand Down

0 comments on commit 169545f

Please sign in to comment.