mirror of
https://github.com/home-assistant/core.git
synced 2025-04-24 01:08:12 +00:00
Adjust version tests (#54391)
* Adjust version tests * patch local import
This commit is contained in:
parent
8ea5a0dbc1
commit
cf8f27bb44
@ -1,31 +1,49 @@
|
||||
"""The test for the version sensor platform."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from pyhaversion import HaVersionSource, exceptions as pyhaversionexceptions
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.version.sensor import ALL_SOURCES
|
||||
from homeassistant.components.version.sensor import HA_VERSION_SOURCES
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.util import dt
|
||||
|
||||
from tests.common import async_fire_time_changed
|
||||
|
||||
MOCK_VERSION = "10.0"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"source",
|
||||
ALL_SOURCES,
|
||||
"source,target_source,name",
|
||||
(
|
||||
(
|
||||
("local", HaVersionSource.LOCAL, "current_version"),
|
||||
("docker", HaVersionSource.CONTAINER, "latest_version"),
|
||||
("hassio", HaVersionSource.SUPERVISOR, "latest_version"),
|
||||
)
|
||||
+ tuple(
|
||||
(source, HaVersionSource(source), "latest_version")
|
||||
for source in HA_VERSION_SOURCES
|
||||
if source != HaVersionSource.LOCAL
|
||||
)
|
||||
),
|
||||
)
|
||||
async def test_version_source(hass, source):
|
||||
async def test_version_source(hass, source, target_source, name):
|
||||
"""Test the Version sensor with different sources."""
|
||||
config = {
|
||||
"sensor": {"platform": "version", "source": source, "image": "qemux86-64"}
|
||||
}
|
||||
|
||||
with patch("pyhaversion.version.HaVersion.version", MOCK_VERSION):
|
||||
with patch("homeassistant.components.version.sensor.HaVersion.get_version"), patch(
|
||||
"homeassistant.components.version.sensor.HaVersion.version", MOCK_VERSION
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
name = "current_version" if source == HaVersionSource.LOCAL else "latest_version"
|
||||
state = hass.states.get(f"sensor.{name}")
|
||||
assert state
|
||||
assert state.attributes["source"] == target_source
|
||||
|
||||
assert state.state == MOCK_VERSION
|
||||
|
||||
@ -34,7 +52,7 @@ async def test_version_fetch_exception(hass, caplog):
|
||||
"""Test fetch exception thrown during updates."""
|
||||
config = {"sensor": {"platform": "version"}}
|
||||
with patch(
|
||||
"pyhaversion.version.HaVersion.get_version",
|
||||
"homeassistant.components.version.sensor.HaVersion.get_version",
|
||||
side_effect=pyhaversionexceptions.HaVersionFetchException(
|
||||
"Fetch exception from pyhaversion"
|
||||
),
|
||||
@ -48,9 +66,35 @@ async def test_version_parse_exception(hass, caplog):
|
||||
"""Test parse exception thrown during updates."""
|
||||
config = {"sensor": {"platform": "version"}}
|
||||
with patch(
|
||||
"pyhaversion.version.HaVersion.get_version",
|
||||
"homeassistant.components.version.sensor.HaVersion.get_version",
|
||||
side_effect=pyhaversionexceptions.HaVersionParseException,
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
assert "Could not parse data received for HaVersionSource.LOCAL" in caplog.text
|
||||
|
||||
|
||||
async def test_update(hass):
|
||||
"""Test updates."""
|
||||
config = {"sensor": {"platform": "version"}}
|
||||
|
||||
with patch("homeassistant.components.version.sensor.HaVersion.get_version"), patch(
|
||||
"homeassistant.components.version.sensor.HaVersion.version", MOCK_VERSION
|
||||
):
|
||||
assert await async_setup_component(hass, "sensor", config)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.current_version")
|
||||
assert state
|
||||
assert state.state == MOCK_VERSION
|
||||
|
||||
with patch("homeassistant.components.version.sensor.HaVersion.get_version"), patch(
|
||||
"homeassistant.components.version.sensor.HaVersion.version", "1234"
|
||||
):
|
||||
|
||||
async_fire_time_changed(hass, dt.utcnow() + timedelta(minutes=5))
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.current_version")
|
||||
assert state
|
||||
assert state.state == "1234"
|
||||
|
Loading…
x
Reference in New Issue
Block a user