From bc196a3c9fbddaf7a00d6d015bb3c84505e780bf Mon Sep 17 00:00:00 2001 From: Malachi Soord Date: Sun, 26 Jan 2020 14:28:42 +0100 Subject: [PATCH] Introduce unique_id for lastfm to allow changing entity_id in backwards compatible way (#31163) * Replace . with _ for lastfm entity_id * lint * double quotes * Rollback change, add unique_id * Expose prop * Generate unique ID from user * Linty * FIx linter * Revert changes for splitting entity_id * Simplify --- homeassistant/components/lastfm/sensor.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/lastfm/sensor.py b/homeassistant/components/lastfm/sensor.py index 68d727626cf..3a830b9f4e6 100644 --- a/homeassistant/components/lastfm/sensor.py +++ b/homeassistant/components/lastfm/sensor.py @@ -1,4 +1,5 @@ """Sensor for Last.fm account status.""" +import hashlib import logging import re @@ -54,8 +55,10 @@ class LastfmSensor(Entity): def __init__(self, user, lastfm_api): """Initialize the sensor.""" + self._unique_id = hashlib.sha256(user.encode("utf-8")).hexdigest() self._user = lastfm_api.get_user(user) self._name = user + self._entity_id = user self._lastfm = lastfm_api self._state = "Not Scrobbling" self._playcount = None @@ -63,6 +66,11 @@ class LastfmSensor(Entity): self._topplayed = None self._cover = None + @property + def unique_id(self): + """Return the unique ID of the sensor.""" + return self._unique_id + @property def name(self): """Return the name of the sensor.""" @@ -71,7 +79,7 @@ class LastfmSensor(Entity): @property def entity_id(self): """Return the entity ID.""" - return f"sensor.lastfm_{self._name}" + return f"sensor.lastfm_{self._entity_id}" @property def state(self):