2017-08-12 08:52:56 +02:00
|
|
|
"""The test for the version sensor platform."""
|
2021-01-01 22:31:56 +01:00
|
|
|
from unittest.mock import patch
|
2017-08-12 08:52:56 +02:00
|
|
|
|
2021-08-10 01:24:18 +02:00
|
|
|
from pyhaversion import HaVersionSource, exceptions as pyhaversionexceptions
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from homeassistant.components.version.sensor import ALL_SOURCES
|
2021-01-01 22:31:56 +01:00
|
|
|
from homeassistant.setup import async_setup_component
|
2020-05-03 11:27:19 -07:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
MOCK_VERSION = "10.0"
|
2017-08-12 08:52:56 +02:00
|
|
|
|
|
|
|
|
2021-08-10 01:24:18 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"source",
|
|
|
|
ALL_SOURCES,
|
|
|
|
)
|
|
|
|
async def test_version_source(hass, source):
|
|
|
|
"""Test the Version sensor with different sources."""
|
|
|
|
config = {
|
|
|
|
"sensor": {"platform": "version", "source": source, "image": "qemux86-64"}
|
|
|
|
}
|
|
|
|
|
|
|
|
with patch("pyhaversion.version.HaVersion.version", MOCK_VERSION):
|
|
|
|
assert await async_setup_component(hass, "sensor", config)
|
|
|
|
await hass.async_block_till_done()
|
2017-08-12 08:52:56 +02:00
|
|
|
|
2021-08-10 01:24:18 +02:00
|
|
|
name = "current_version" if source == HaVersionSource.LOCAL else "latest_version"
|
|
|
|
state = hass.states.get(f"sensor.{name}")
|
2017-08-12 08:52:56 +02:00
|
|
|
|
2021-08-10 01:24:18 +02:00
|
|
|
assert state.state == MOCK_VERSION
|
2017-08-12 08:52:56 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2021-08-10 01:24:18 +02:00
|
|
|
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",
|
|
|
|
side_effect=pyhaversionexceptions.HaVersionFetchException(
|
|
|
|
"Fetch exception from pyhaversion"
|
|
|
|
),
|
|
|
|
):
|
2020-04-07 18:36:35 +02:00
|
|
|
assert await async_setup_component(hass, "sensor", config)
|
|
|
|
await hass.async_block_till_done()
|
2021-08-10 01:24:18 +02:00
|
|
|
assert "Fetch exception from pyhaversion" in caplog.text
|
2017-08-12 08:52:56 +02:00
|
|
|
|
|
|
|
|
2021-08-10 01:24:18 +02:00
|
|
|
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",
|
|
|
|
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
|