Skip to content

Commit

Permalink
Prevent last.fm errors with None (home-assistant#33446)
Browse files Browse the repository at this point in the history
  • Loading branch information
inverse authored Apr 5, 2020
1 parent fca90a8 commit 3f0936f
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 10 deletions.
30 changes: 20 additions & 10 deletions homeassistant/components/lastfm/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/components/lastfm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""The tests for lastfm."""
82 changes: 82 additions & 0 deletions tests/components/lastfm/test_sensor.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 3f0936f

Please sign in to comment.