forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent last.fm errors with None (home-assistant#33446)
- Loading branch information
Showing
4 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""The tests for lastfm.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |