diff --git a/homeassistant/components/discogs/manifest.json b/homeassistant/components/discogs/manifest.json index 53dc30d6b39..2d8e308a42b 100644 --- a/homeassistant/components/discogs/manifest.json +++ b/homeassistant/components/discogs/manifest.json @@ -2,6 +2,6 @@ "domain": "discogs", "name": "Discogs", "documentation": "https://www.home-assistant.io/integrations/discogs", - "requirements": ["discogs_client==2.2.2"], + "requirements": ["discogs_client==2.3.0"], "codeowners": ["@thibmaek"] } diff --git a/homeassistant/components/discogs/sensor.py b/homeassistant/components/discogs/sensor.py index 40f27135be1..4d78b540a0c 100644 --- a/homeassistant/components/discogs/sensor.py +++ b/homeassistant/components/discogs/sensor.py @@ -122,22 +122,22 @@ class DiscogsSensor(Entity): @property def device_state_attributes(self): - """Return the state attributes of the sensor.""" + """Return the device state attributes of the sensor.""" if self._state is None or self._attrs is None: return None - if self._type != SENSOR_RANDOM_RECORD_TYPE: + if self._type == SENSOR_RANDOM_RECORD_TYPE and self._state is not None: return { + "cat_no": self._attrs["labels"][0]["catno"], + "cover_image": self._attrs["cover_image"], + "format": f"{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"], } return { - "cat_no": self._attrs["labels"][0]["catno"], - "cover_image": self._attrs["cover_image"], - "format": f"{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"], } @@ -146,11 +146,14 @@ class DiscogsSensor(Entity): """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] - random_index = random.randrange(collection.count) - random_record = collection.releases[random_index].release + if collection.count > 0: + random_index = random.randrange(collection.count) + random_record = collection.releases[random_index].release - self._attrs = random_record.data - return f"{random_record.data['artists'][0]['name']} - {random_record.data['title']}" + self._attrs = random_record.data + return f"{random_record.data['artists'][0]['name']} - {random_record.data['title']}" + + return None def update(self): """Set state to the amount of records in user's collection.""" diff --git a/requirements_all.txt b/requirements_all.txt index 34bac40ade0..8fdc47ccc8a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -485,7 +485,7 @@ devolo-home-control-api==0.13.0 directv==0.3.0 # homeassistant.components.discogs -discogs_client==2.2.2 +discogs_client==2.3.0 # homeassistant.components.discord discord.py==1.4.1