1 """Show the amount of records in a user's Discogs collection."""
3 from __future__
import annotations
5 from datetime
import timedelta
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
15 SensorEntityDescription,
24 _LOGGER = logging.getLogger(__name__)
26 ATTR_IDENTITY =
"identity"
28 DEFAULT_NAME =
"Discogs"
30 ICON_RECORD =
"mdi:album"
31 ICON_PLAYER =
"mdi:record-player"
32 UNIT_RECORDS =
"records"
36 SENSOR_COLLECTION_TYPE =
"collection"
37 SENSOR_WANTLIST_TYPE =
"wantlist"
38 SENSOR_RANDOM_RECORD_TYPE =
"random_record"
40 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
42 key=SENSOR_COLLECTION_TYPE,
45 native_unit_of_measurement=UNIT_RECORDS,
48 key=SENSOR_WANTLIST_TYPE,
51 native_unit_of_measurement=UNIT_RECORDS,
54 key=SENSOR_RANDOM_RECORD_TYPE,
59 SENSOR_KEYS: list[str] = [desc.key
for desc
in SENSOR_TYPES]
61 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
63 vol.Required(CONF_TOKEN): cv.string,
64 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
65 vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_KEYS): vol.All(
66 cv.ensure_list, [vol.In(SENSOR_KEYS)]
75 add_entities: AddEntitiesCallback,
76 discovery_info: DiscoveryInfoType |
None =
None,
78 """Set up the Discogs sensor."""
79 token = config[CONF_TOKEN]
80 name = config[CONF_NAME]
83 _discogs_client = discogs_client.Client(SERVER_SOFTWARE, user_token=token)
86 "user": _discogs_client.identity().name,
87 "folders": _discogs_client.identity().collection_folders,
88 "collection_count": _discogs_client.identity().num_collection,
89 "wantlist_count": _discogs_client.identity().num_wantlist,
91 except discogs_client.exceptions.HTTPError:
92 _LOGGER.error(
"API token is not valid")
95 monitored_conditions = config[CONF_MONITORED_CONDITIONS]
98 for description
in SENSOR_TYPES
99 if description.key
in monitored_conditions
106 """Create a new Discogs sensor for a specific type."""
108 _attr_attribution =
"Data provided by Discogs"
111 self, discogs_data, name, description: SensorEntityDescription
113 """Initialize the Discogs sensor."""
116 self.
_attrs_attrs: dict = {}
122 """Return the device state attributes of the sensor."""
131 "cat_no": self.
_attrs_attrs[
"labels"][0][
"catno"],
132 "cover_image": self.
_attrs_attrs[
"cover_image"],
134 f
"{self._attrs['formats'][0]['name']} ({self._attrs['formats'][0]['descriptions'][0]})"
136 "label": self.
_attrs_attrs[
"labels"][0][
"name"],
137 "released": self.
_attrs_attrs[
"year"],
146 """Get a random record suggestion from the user's collection."""
149 if collection.count > 0:
150 random_index = random.randrange(collection.count)
151 random_record = collection.releases[random_index].release
155 f
"{random_record.data['artists'][0]['name']} -"
156 f
" {random_record.data['title']}"
162 """Set state to the amount of records in user's collection."""
def get_random_record(self)
def extra_state_attributes(self)
None __init__(self, discogs_data, name, SensorEntityDescription description)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def add_entities(account, async_add_entities, tracked)