Skip to content

Commit

Permalink
update plugin updater tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pssc committed Sep 16, 2024
1 parent c8fc8ee commit 82de31d
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 6 deletions.
1 change: 1 addition & 0 deletions tests/components/squeezebox/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
STATUS_SENSOR_INFO_TOTAL_SONGS: 42,
STATUS_SENSOR_PLAYER_COUNT: 10,
STATUS_SENSOR_OTHER_PLAYER_COUNT: 0,
"_can": 1,
"players_loop": [
{
"isplaying": 0,
Expand Down
132 changes: 126 additions & 6 deletions tests/components/squeezebox/test_update.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
"""Test squeezebox update platform."""

import copy
from datetime import timedelta
from unittest.mock import patch

from homeassistant.const import Platform
from homeassistant.components.squeezebox.const import (
SENSOR_UPDATE_INTERVAL,
STATUS_SENSOR_NEWPLUGINS,
)
from homeassistant.components.update import (
ATTR_IN_PROGRESS,
DOMAIN as UPDATE_DOMAIN,
SERVICE_INSTALL,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, Platform
from homeassistant.core import HomeAssistant
from homeassistant.util import dt as dt_util

from .conftest import FAKE_QUERY_RESPONSE

from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, async_fire_time_changed


async def test_update_lms(
Expand All @@ -33,15 +44,16 @@ async def test_update_lms(
state = hass.states.get("update.fakelib_lyrion_music_server")

assert state is not None
assert state.state == "on"
assert state.state == STATE_ON


async def test_update_plugins(
async def test_update_plugins_install_fallback(
hass: HomeAssistant,
config_entry: MockConfigEntry,
) -> None:
"""Test binary sensor states and attributes."""

entity_id = "update.fakelib_updated_plugins"
# Setup component
with (
patch(
Expand All @@ -55,7 +67,115 @@ async def test_update_plugins(
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done(wait_background_tasks=True)
state = hass.states.get("update.fakelib_updated_plugins")

state = hass.states.get(entity_id)
assert state is not None
assert state.state == "on"
assert state.state == STATE_ON

polltime = 30
with (
patch(
"homeassistant.components.squeezebox.Server.async_query",
return_value=False,
),
patch(
"homeassistant.components.squeezebox.update.POLL_AFTER_INSTALL",
polltime,
),
):
await hass.services.async_call(
UPDATE_DOMAIN,
SERVICE_INSTALL,
{
ATTR_ENTITY_ID: entity_id,
},
blocking=True,
)

state = hass.states.get(entity_id)
attrs = state.attributes
assert attrs[ATTR_IN_PROGRESS]

with (
patch(
"homeassistant.components.squeezebox.Server.async_status",
return_value=copy.deepcopy(FAKE_QUERY_RESPONSE),
),
):
async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=polltime + 1),
)
await hass.async_block_till_done(wait_background_tasks=True)

state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_ON

attrs = state.attributes
assert not attrs[ATTR_IN_PROGRESS]


async def test_update_plugins_install_ok(
hass: HomeAssistant,
config_entry: MockConfigEntry,
) -> None:
"""Test binary sensor states and attributes."""

entity_id = "update.fakelib_updated_plugins"
# Setup component
with (
patch(
"homeassistant.components.squeezebox.PLATFORMS",
[Platform.UPDATE],
),
patch(
"homeassistant.components.squeezebox.Server.async_query",
return_value=copy.deepcopy(FAKE_QUERY_RESPONSE),
),
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done(wait_background_tasks=True)

with (
patch(
"homeassistant.components.squeezebox.Server.async_query",
return_value=False,
),
):
await hass.services.async_call(
UPDATE_DOMAIN,
SERVICE_INSTALL,
{
ATTR_ENTITY_ID: entity_id,
},
blocking=True,
)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_ON

attrs = state.attributes
assert attrs[ATTR_IN_PROGRESS]

resp = copy.deepcopy(FAKE_QUERY_RESPONSE)
del resp[STATUS_SENSOR_NEWPLUGINS]

with (
patch(
"homeassistant.components.squeezebox.Server.async_status",
return_value=resp,
),
):
async_fire_time_changed(
hass,
dt_util.utcnow() + timedelta(seconds=SENSOR_UPDATE_INTERVAL + 1),
)
await hass.async_block_till_done(wait_background_tasks=True)

state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_OFF

attrs = state.attributes
assert not attrs[ATTR_IN_PROGRESS]

0 comments on commit 82de31d

Please sign in to comment.