1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00
This commit is contained in:
Paulus Schoutsen
2019-07-31 12:25:30 -07:00
parent da05dfe708
commit 4de97abc3a
2676 changed files with 163166 additions and 140084 deletions

View File

@@ -7,53 +7,60 @@ import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_ATTRIBUTION, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_TOKEN)
ATTR_ATTRIBUTION,
CONF_MONITORED_CONDITIONS,
CONF_NAME,
CONF_TOKEN,
)
from homeassistant.helpers.aiohttp_client import SERVER_SOFTWARE
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
ATTR_IDENTITY = 'identity'
ATTR_IDENTITY = "identity"
ATTRIBUTION = "Data provided by Discogs"
DEFAULT_NAME = 'Discogs'
DEFAULT_NAME = "Discogs"
ICON_RECORD = 'mdi:album'
ICON_PLAYER = 'mdi:record-player'
UNIT_RECORDS = 'records'
ICON_RECORD = "mdi:album"
ICON_PLAYER = "mdi:record-player"
UNIT_RECORDS = "records"
SCAN_INTERVAL = timedelta(minutes=10)
SENSOR_COLLECTION_TYPE = 'collection'
SENSOR_WANTLIST_TYPE = 'wantlist'
SENSOR_RANDOM_RECORD_TYPE = 'random_record'
SENSOR_COLLECTION_TYPE = "collection"
SENSOR_WANTLIST_TYPE = "wantlist"
SENSOR_RANDOM_RECORD_TYPE = "random_record"
SENSORS = {
SENSOR_COLLECTION_TYPE: {
'name': 'Collection',
'icon': ICON_RECORD,
'unit_of_measurement': UNIT_RECORDS
"name": "Collection",
"icon": ICON_RECORD,
"unit_of_measurement": UNIT_RECORDS,
},
SENSOR_WANTLIST_TYPE: {
'name': 'Wantlist',
'icon': ICON_RECORD,
'unit_of_measurement': UNIT_RECORDS
"name": "Wantlist",
"icon": ICON_RECORD,
"unit_of_measurement": UNIT_RECORDS,
},
SENSOR_RANDOM_RECORD_TYPE: {
'name': 'Random Record',
'icon': ICON_PLAYER,
'unit_of_measurement': None
"name": "Random Record",
"icon": ICON_PLAYER,
"unit_of_measurement": None,
},
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_TOKEN): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSORS)):
vol.All(cv.ensure_list, [vol.In(SENSORS)])
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_TOKEN): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSORS)): vol.All(
cv.ensure_list, [vol.In(SENSORS)]
),
}
)
def setup_platform(hass, config, add_entities, discovery_info=None):
@@ -64,14 +71,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
name = config[CONF_NAME]
try:
discogs_client = discogs_client.Client(
SERVER_SOFTWARE, user_token=token)
discogs_client = discogs_client.Client(SERVER_SOFTWARE, user_token=token)
discogs_data = {
'user': discogs_client.identity().name,
'folders': discogs_client.identity().collection_folders,
'collection_count': discogs_client.identity().num_collection,
'wantlist_count': discogs_client.identity().num_wantlist
"user": discogs_client.identity().name,
"folders": discogs_client.identity().collection_folders,
"collection_count": discogs_client.identity().num_collection,
"wantlist_count": discogs_client.identity().num_wantlist,
}
except discogs_client.exceptions.HTTPError:
_LOGGER.error("API token is not valid")
@@ -98,7 +104,7 @@ class DiscogsSensor(Entity):
@property
def name(self):
"""Return the name of the sensor."""
return "{} {}".format(self._name, SENSORS[self._type]['name'])
return "{} {}".format(self._name, SENSORS[self._type]["name"])
@property
def state(self):
@@ -108,12 +114,12 @@ class DiscogsSensor(Entity):
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return SENSORS[self._type]['icon']
return SENSORS[self._type]["icon"]
@property
def unit_of_measurement(self):
"""Return the unit this state is expressed in."""
return SENSORS[self._type]['unit_of_measurement']
return SENSORS[self._type]["unit_of_measurement"]
@property
def device_state_attributes(self):
@@ -124,38 +130,39 @@ class DiscogsSensor(Entity):
if self._type != SENSOR_RANDOM_RECORD_TYPE:
return {
ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_IDENTITY: self._discogs_data['user'],
ATTR_IDENTITY: self._discogs_data["user"],
}
return {
'cat_no': self._attrs['labels'][0]['catno'],
'cover_image': self._attrs['cover_image'],
'format': "{} ({})".format(
self._attrs['formats'][0]['name'],
self._attrs['formats'][0]['descriptions'][0]),
'label': self._attrs['labels'][0]['name'],
'released': self._attrs['year'],
"cat_no": self._attrs["labels"][0]["catno"],
"cover_image": self._attrs["cover_image"],
"format": "{} ({})".format(
self._attrs["formats"][0]["name"],
self._attrs["formats"][0]["descriptions"][0],
),
"label": self._attrs["labels"][0]["name"],
"released": self._attrs["year"],
ATTR_ATTRIBUTION: ATTRIBUTION,
ATTR_IDENTITY: self._discogs_data['user'],
ATTR_IDENTITY: self._discogs_data["user"],
}
def get_random_record(self):
"""Get a random record suggestion from the user's collection."""
# Index 0 in the folders is the 'All' folder
collection = self._discogs_data['folders'][0]
collection = self._discogs_data["folders"][0]
random_index = random.randrange(collection.count)
random_record = collection.releases[random_index].release
self._attrs = random_record.data
return "{} - {}".format(
random_record.data['artists'][0]['name'],
random_record.data['title'])
random_record.data["artists"][0]["name"], random_record.data["title"]
)
def update(self):
"""Set state to the amount of records in user's collection."""
if self._type == SENSOR_COLLECTION_TYPE:
self._state = self._discogs_data['collection_count']
self._state = self._discogs_data["collection_count"]
elif self._type == SENSOR_WANTLIST_TYPE:
self._state = self._discogs_data['wantlist_count']
self._state = self._discogs_data["wantlist_count"]
else:
self._state = self.get_random_record()