Skip to content

Commit

Permalink
- Use async_added_to_hass in media player
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
jkeljo committed Jul 19, 2018
1 parent af670bb commit 383566b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/light/sisyphus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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):
Expand All @@ -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."""
Expand Down
37 changes: 16 additions & 21 deletions homeassistant/components/media_player/sisyphus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
16 changes: 10 additions & 6 deletions homeassistant/components/sisyphus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)

Expand All @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 383566b

Please sign in to comment.