Skip to content

Commit

Permalink
fix: stagger loading of components
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Apr 19, 2020
1 parent 75faa7f commit 0ddda71
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
3 changes: 2 additions & 1 deletion custom_components/alexa_media/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
MIN_TIME_BETWEEN_SCANS = SCAN_INTERVAL
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)

ALEXA_COMPONENTS = ["media_player", "notify", "alarm_control_panel", "switch", "sensor"]
ALEXA_COMPONENTS = ["media_player", "alarm_control_panel"]
DEPENDENT_ALEXA_COMPONENTS = ["notify", "switch", "sensor"]

CONF_ACCOUNTS = "accounts"
CONF_DEBUG = "debug"
Expand Down
34 changes: 30 additions & 4 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@
STATE_STANDBY,
STATE_UNAVAILABLE,
)
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.event import async_call_later

from . import (
CONF_EMAIL,
CONF_NAME,
CONF_PASSWORD,
DATA_ALEXAMEDIA,
DOMAIN as ALEXA_DOMAIN,
MIN_TIME_BETWEEN_FORCED_SCANS,
MIN_TIME_BETWEEN_SCANS,
async_load_platform,
hide_email,
hide_serial,
)
from .const import PLAY_SCAN_INTERVAL
from .const import DEPENDENT_ALEXA_COMPONENTS, PLAY_SCAN_INTERVAL
from .helpers import _catch_login_errors, add_devices, retry_async

SUPPORT_ALEXA = (
Expand All @@ -70,7 +74,7 @@
DEPENDENCIES = [ALEXA_DOMAIN]


@retry_async(limit=5, delay=2, catch_exceptions=True)
# @retry_async(limit=5, delay=2, catch_exceptions=True)
async def async_setup_platform(hass, config, add_devices_callback, discovery_info=None):
# pylint: disable=unused-argument
"""Set up the Alexa media player platform."""
Expand Down Expand Up @@ -99,9 +103,31 @@ async def async_setup_platform(hass, config, add_devices_callback, discovery_inf

async def async_setup_entry(hass, config_entry, async_add_devices):
"""Set up the Alexa media player platform by config_entry."""
return await async_setup_platform(
if await async_setup_platform(
hass, config_entry.data, async_add_devices, discovery_info=None
)
):
for component in DEPENDENT_ALEXA_COMPONENTS:
if component == "notify":
cleaned_config = config_entry.data.copy()
cleaned_config.pop(CONF_PASSWORD, None)
# CONF_PASSWORD contains sensitive info which is no longer needed
hass.async_create_task(
async_load_platform(
hass,
component,
ALEXA_DOMAIN,
{CONF_NAME: ALEXA_DOMAIN, "config": cleaned_config},
cleaned_config,
)
)
else:
hass.async_add_job(
hass.config_entries.async_forward_entry_setup(
config_entry, component
)
)
return True
raise ConfigEntryNotReady


async def async_unload_entry(hass, entry) -> bool:
Expand Down
15 changes: 3 additions & 12 deletions custom_components/alexa_media/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import List, Text # noqa pylint: disable=unused-import

from homeassistant.const import DEVICE_CLASS_TIMESTAMP, STATE_UNAVAILABLE
from homeassistant.exceptions import NoEntitySpecifiedError
from homeassistant.exceptions import ConfigEntryNotReady, NoEntitySpecifiedError
from homeassistant.helpers.entity import Entity
from homeassistant.util import dt
import pytz
Expand Down Expand Up @@ -61,7 +61,6 @@
}


@retry_async(limit=5, delay=5, catch_exceptions=False)
async def async_setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Set up the Alexa sensor platform."""
devices: List[AlexaMediaNotificationSensor] = []
Expand All @@ -84,15 +83,7 @@ async def async_setup_platform(hass, config, add_devices_callback, discovery_inf
hide_email(account),
hide_serial(key),
)
if devices:
await add_devices(
hide_email(account),
devices,
add_devices_callback,
include_filter,
exclude_filter,
)
return False
raise ConfigEntryNotReady
if key not in (account_dict["entities"]["sensor"]):
(account_dict["entities"]["sensor"][key]) = {}
for (n_type, class_) in SENSOR_TYPES.items():
Expand Down Expand Up @@ -243,7 +234,7 @@ def _fix_alarm_date_time(self, value):
return value

def _update_recurring_alarm(self, value):
_LOGGER.debug("value %s", value)
_LOGGER.debug("Sensor value %s", value)
alarm = value[1][self._sensor_property]
reminder = None
if isinstance(value[1][self._sensor_property], int):
Expand Down
13 changes: 2 additions & 11 deletions custom_components/alexa_media/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import List # noqa pylint: disable=unused-import

from homeassistant.components.switch import SwitchDevice
from homeassistant.exceptions import NoEntitySpecifiedError
from homeassistant.exceptions import ConfigEntryNotReady, NoEntitySpecifiedError

from . import (
CONF_EMAIL,
Expand All @@ -27,7 +27,6 @@
_LOGGER = logging.getLogger(__name__)


@retry_async(limit=5, delay=5, catch_exceptions=True)
async def async_setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Set up the Alexa switch platform."""
devices = [] # type: List[DNDSwitch]
Expand All @@ -50,15 +49,7 @@ async def async_setup_platform(hass, config, add_devices_callback, discovery_inf
hide_email(account),
hide_serial(key),
)
if devices:
await add_devices(
hide_email(account),
devices,
add_devices_callback,
include_filter,
exclude_filter,
)
return False
raise ConfigEntryNotReady
if key not in (
hass.data[DATA_ALEXAMEDIA]["accounts"][account]["entities"]["switch"]
):
Expand Down

0 comments on commit 0ddda71

Please sign in to comment.