diff --git a/homeassistant/components/lastfm/sensor.py b/homeassistant/components/lastfm/sensor.py index 80a72f1d6fda81..4783457112cbb5 100644 --- a/homeassistant/components/lastfm/sensor.py +++ b/homeassistant/components/lastfm/sensor.py @@ -19,6 +19,8 @@ ATTR_TOP_PLAYED = "top_played" ATTRIBUTION = "Data provided by Last.fm" +STATE_NOT_SCROBBLING = "Not Scrobbling" + CONF_USERS = "users" ICON = "mdi:lastfm" @@ -84,17 +86,25 @@ def update(self): """Update device state.""" self._cover = self._user.get_image() self._playcount = self._user.get_playcount() - last = self._user.get_recent_tracks(limit=2)[0] - self._lastplayed = f"{last.track.artist} - {last.track.title}" - top = self._user.get_top_tracks(limit=1)[0] - toptitle = re.search("', '(.+?)',", str(top)) - topartist = re.search("'(.+?)',", str(top)) - self._topplayed = f"{topartist.group(1)} - {toptitle.group(1)}" - if self._user.get_now_playing() is None: - self._state = "Not Scrobbling" + + recent_tracks = self._user.get_recent_tracks(limit=2) + if recent_tracks: + last = recent_tracks[0] + self._lastplayed = f"{last.track.artist} - {last.track.title}" + + top_tracks = self._user.get_top_tracks(limit=1) + if top_tracks: + top = top_tracks[0] + toptitle = re.search("', '(.+?)',", str(top)) + topartist = re.search("'(.+?)',", str(top)) + self._topplayed = f"{topartist.group(1)} - {toptitle.group(1)}" + + now_playing = self._user.get_now_playing() + if now_playing is None: + self._state = STATE_NOT_SCROBBLING return - now = self._user.get_now_playing() - self._state = f"{now.artist} - {now.title}" + + self._state = f"{now_playing.artist} - {now_playing.title}" @property def device_state_attributes(self): diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 5db1635e39a750..b064ca1b411718 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -532,6 +532,9 @@ pyiqvia==0.2.1 # homeassistant.components.kira pykira==0.1.1 +# homeassistant.components.lastfm +pylast==3.2.1 + # homeassistant.components.linky pylinky==0.4.0 diff --git a/tests/components/lastfm/__init__.py b/tests/components/lastfm/__init__.py new file mode 100644 index 00000000000000..4e7cfffa833a51 --- /dev/null +++ b/tests/components/lastfm/__init__.py @@ -0,0 +1 @@ +"""The tests for lastfm.""" diff --git a/tests/components/lastfm/test_sensor.py b/tests/components/lastfm/test_sensor.py new file mode 100644 index 00000000000000..c2ce8e947dd689 --- /dev/null +++ b/tests/components/lastfm/test_sensor.py @@ -0,0 +1,82 @@ +"""Tests for the lastfm sensor.""" +from unittest.mock import patch + +from pylast import Track +import pytest + +from homeassistant.components import sensor +from homeassistant.components.lastfm.sensor import STATE_NOT_SCROBBLING +from homeassistant.setup import async_setup_component + + +class MockUser: + """Mock user object for pylast.""" + + def __init__(self, now_playing_result): + """Initialize the mock.""" + self._now_playing_result = now_playing_result + + def get_playcount(self): + """Get mock play count.""" + return 1 + + def get_image(self): + """Get mock image.""" + pass + + def get_recent_tracks(self, limit): + """Get mock recent tracks.""" + return [] + + def get_top_tracks(self, limit): + """Get mock top tracks.""" + return [] + + def get_now_playing(self): + """Get mock now playing.""" + return self._now_playing_result + + +@pytest.fixture(name="lastfm_network") +def lastfm_network_fixture(): + """Create fixture for LastFMNetwork.""" + with patch("pylast.LastFMNetwork") as lastfm_network: + yield lastfm_network + + +async def test_update_not_playing(hass, lastfm_network): + """Test update when no playing song.""" + + lastfm_network.return_value.get_user.return_value = MockUser(None) + + assert await async_setup_component( + hass, + sensor.DOMAIN, + {"sensor": {"platform": "lastfm", "api_key": "secret-key", "users": ["test"]}}, + ) + + entity_id = "sensor.test" + + state = hass.states.get(entity_id) + + assert state.state == STATE_NOT_SCROBBLING + + +async def test_update_playing(hass, lastfm_network): + """Test update when song playing.""" + + lastfm_network.return_value.get_user.return_value = MockUser( + Track("artist", "title", None) + ) + + assert await async_setup_component( + hass, + sensor.DOMAIN, + {"sensor": {"platform": "lastfm", "api_key": "secret-key", "users": ["test"]}}, + ) + + entity_id = "sensor.test" + + state = hass.states.get(entity_id) + + assert state.state == "artist - title"