Check if API key is valid and users available (#16494)

This commit is contained in:
Fabian Affolter 2018-09-08 18:33:41 +02:00 committed by Martin Hjelmare
parent 93143384a8
commit 9944c60311

View File

@ -4,6 +4,7 @@ Sensor for Last.fm account status.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.lastfm/ https://home-assistant.io/components/sensor.lastfm/
""" """
import logging
import re import re
import voluptuous as vol import voluptuous as vol
@ -15,6 +16,8 @@ from homeassistant.helpers.entity import Entity
REQUIREMENTS = ['pylast==2.4.0'] REQUIREMENTS = ['pylast==2.4.0']
_LOGGER = logging.getLogger(__name__)
ATTR_LAST_PLAYED = 'last_played' ATTR_LAST_PLAYED = 'last_played'
ATTR_PLAY_COUNT = 'play_count' ATTR_PLAY_COUNT = 'play_count'
ATTR_TOP_PLAYED = 'top_played' ATTR_TOP_PLAYED = 'top_played'
@ -30,13 +33,25 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Last.fm platform.""" """Set up the Last.fm sensor platform."""
import pylast as lastfm import pylast as lastfm
network = lastfm.LastFMNetwork(api_key=config.get(CONF_API_KEY)) from pylast import WSError
add_entities( api_key = config[CONF_API_KEY]
[LastfmSensor( users = config.get(CONF_USERS)
username, network) for username in config.get(CONF_USERS)], True)
lastfm_api = lastfm.LastFMNetwork(api_key=api_key)
entities = []
for username in users:
try:
lastfm_api.get_user(username).get_image()
entities.append(LastfmSensor(username, lastfm_api))
except WSError as error:
_LOGGER.error(error)
return
add_entities(entities, True)
class LastfmSensor(Entity): class LastfmSensor(Entity):