From 383566ba339d029ce5c9e1b858e5c92a65dc16e5 Mon Sep 17 00:00:00 2001 From: Jonathan Keljo Date: Mon, 16 Jul 2018 19:16:07 -0700 Subject: [PATCH] - Use async_added_to_hass in media player - async_schedule_update_ha_state in listeners - constants for supported features - subscripting for required keys - asyncio.wait - update to sisyphus-control with passed-in session --- homeassistant/components/light/sisyphus.py | 6 ++- .../components/media_player/sisyphus.py | 37 ++++++++----------- homeassistant/components/sisyphus.py | 16 +++++--- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/light/sisyphus.py b/homeassistant/components/light/sisyphus.py index 4d5f35bc8bb347..ded78716317161 100644 --- a/homeassistant/components/light/sisyphus.py +++ b/homeassistant/components/light/sisyphus.py @@ -14,6 +14,8 @@ DEPENDENCIES = ['sisyphus'] +SUPPORTED_FEATURES = SUPPORT_BRIGHTNESS + def setup_platform(hass, config, add_devices, discovery_info=None): """Set up a single Sisyphus table.""" @@ -39,7 +41,7 @@ def __init__(self, name, table): async def async_added_to_hass(self): """Add listeners after this object has been initialized.""" self._table.add_listener( - lambda: self.schedule_update_ha_state(False)) + lambda: self.async_schedule_update_ha_state(False)) @property def name(self): @@ -59,7 +61,7 @@ def brightness(self): @property def supported_features(self): """Return the features supported by the table; i.e. brightness.""" - return SUPPORT_BRIGHTNESS + return SUPPORTED_FEATURES async def async_turn_off(self, **kwargs): """Put the table to sleep.""" diff --git a/homeassistant/components/media_player/sisyphus.py b/homeassistant/components/media_player/sisyphus.py index 7ade523b4db00a..db5d823ea3fe8b 100644 --- a/homeassistant/components/media_player/sisyphus.py +++ b/homeassistant/components/media_player/sisyphus.py @@ -27,6 +27,16 @@ MEDIA_TYPE_TRACK = "sisyphus_track" +SUPPORTED_FEATURES = SUPPORT_VOLUME_MUTE \ + | SUPPORT_VOLUME_SET \ + | SUPPORT_TURN_OFF \ + | SUPPORT_TURN_ON \ + | SUPPORT_PAUSE \ + | SUPPORT_SHUFFLE_SET \ + | SUPPORT_PREVIOUS_TRACK \ + | SUPPORT_NEXT_TRACK \ + | SUPPORT_PLAY + # pylint: disable=unused-argument def setup_platform(hass, config, add_devices, discovery_info=None): @@ -52,18 +62,11 @@ def __init__(self, name, host, table): self._name = name self._host = host self._table = table - self._initialized = False - - def update(self): - """Lazily initializes the table.""" - if not self._initialized: - # We wait until update before adding the listener because - # otherwise there's a race condition by which this entity - # might not have had its hass field set, and thus - # the schedule_update_ha_state call will fail - self._table.add_listener( - lambda: self.schedule_update_ha_state(False)) - self._initialized = True + + async def async_added_to_hass(self): + """Add listeners after this object has been initialized.""" + self._table.add_listener( + lambda: self.async_schedule_update_ha_state(False)) @property def name(self): @@ -132,15 +135,7 @@ def media_content_id(self): @property def supported_features(self): """Return the features supported by this table.""" - return SUPPORT_VOLUME_MUTE \ - | SUPPORT_VOLUME_SET \ - | SUPPORT_TURN_OFF \ - | SUPPORT_TURN_ON \ - | SUPPORT_PAUSE \ - | SUPPORT_SHUFFLE_SET \ - | SUPPORT_PREVIOUS_TRACK \ - | SUPPORT_NEXT_TRACK \ - | SUPPORT_PLAY + return SUPPORTED_FEATURES @property def media_image_url(self): diff --git a/homeassistant/components/sisyphus.py b/homeassistant/components/sisyphus.py index 85281d539d034f..dc9f9cc4c2573d 100644 --- a/homeassistant/components/sisyphus.py +++ b/homeassistant/components/sisyphus.py @@ -4,6 +4,7 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/sisyphus/ """ +import asyncio import logging import voluptuous as vol @@ -13,10 +14,11 @@ CONF_NAME, EVENT_HOMEASSISTANT_STOP ) +from homeassistant.helpers.aiohttp_client import async_get_clientsession import homeassistant.helpers.config_validation as cv from homeassistant.helpers.discovery import async_load_platform -REQUIREMENTS = ['sisyphus-control==2.0.0'] +REQUIREMENTS = ['sisyphus-control==2.1'] _LOGGER = logging.getLogger(__name__) @@ -42,10 +44,11 @@ async def async_setup(hass, config): from sisyphus_control import Table tables = hass.data.setdefault(DATA_SISYPHUS, {}) table_configs = config.get(DOMAIN) + session = async_get_clientsession(hass) async def add_table(host, name=None): """Add platforms for a single table with the given hostname.""" - table = await Table.connect(host) + table = await Table.connect(host, session) if name is None: name = table.name tables[name] = table @@ -64,16 +67,17 @@ async def add_table(host, name=None): )) if isinstance(table_configs, dict): # AUTODETECT_SCHEMA - for ip_address in await Table.find_table_ips(): + for ip_address in await Table.find_table_ips(session): await add_table(ip_address) else: # TABLES_SCHEMA for conf in table_configs: - await add_table(conf.get(CONF_HOST), conf.get(CONF_NAME)) + await add_table(conf[CONF_HOST], conf[CONF_NAME]) async def close_tables(*args): """Close all table objects.""" - for table in tables.values(): - await table.close() + tasks = [table.close() for table in tables.values()] + if tasks: + await asyncio.wait(tasks) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, close_tables)