Prevent last.fm errors with None (#33446)

This commit is contained in:
Malachi Soord 2020-04-05 18:03:13 +02:00 committed by GitHub
parent fca90a8ddc
commit 3f0936f068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 10 deletions

View File

@ -19,6 +19,8 @@ ATTR_PLAY_COUNT = "play_count"
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 @@ class LastfmSensor(Entity):
"""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):

View File

@ -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

View File

@ -0,0 +1 @@
"""The tests for lastfm."""

View File

@ -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"