Exemplo n.º 1
0
async def test_local(caplog: pytest.LogCaptureFixture):
    """Test ha.io/version.json stable."""
    async with aiohttp.ClientSession() as session:
        haversion = HaVersion(session=session, source=HaVersionSource.LOCAL)
        await haversion.get_version()
        assert haversion.version is None
        assert "No homeassistant installation found" in caplog.text

    with patch("pyhaversion.local.localversion", STABLE_VERSION):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(session=session,
                                  source=HaVersionSource.LOCAL)
            await haversion.get_version()
            assert haversion.version == STABLE_VERSION
Exemplo n.º 2
0
async def test_beta_version_beta_week_pagination(aresponses):
    """Test docker beta during beta week."""
    aresponses.add(
        "registry.hub.docker.com",
        "/v2/repositories/homeassistant/home-assistant/tags",
        "get",
        aresponses.Response(
            text=fixture("docker/beta_week_page1", False),
            status=200,
            headers=HEADERS,
        ),
    )
    aresponses.add(
        "registry.hub.docker.com",
        "/v2/repositories/homeassistant/home-assistant/tags/page2",
        "get",
        aresponses.Response(
            text=fixture("docker/beta_week_page2", False),
            status=200,
            headers=HEADERS,
        ),
    )

    async with aiohttp.ClientSession() as session:
        haversion = HaVersion(
            session=session,
            source=HaVersionSource.DOCKER,
            channel=HaVersionChannel.BETA,
        )
        await haversion.get_version()
        assert haversion.version == BETA_VERSION_BETA_WEEK
Exemplo n.º 3
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up the version integration from a config entry."""

    board = entry.data[CONF_BOARD]

    if board not in BOARD_MAP:
        _LOGGER.error(
            'Board "%s" is (no longer) valid. Please remove the integration "%s"',
            board,
            entry.title,
        )
        return False

    coordinator = VersionDataUpdateCoordinator(
        hass=hass,
        api=HaVersion(
            session=async_get_clientsession(hass),
            source=entry.data[CONF_SOURCE],
            image=entry.data[CONF_IMAGE],
            board=BOARD_MAP[board],
            channel=entry.data[CONF_CHANNEL].lower(),
        ),
    )
    await coordinator.async_config_entry_first_refresh()

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
Exemplo n.º 4
0
async def test_stable_version_pagination(aresponses):
    """Test container beta during beta week."""
    aresponses.add(
        "registry.hub.docker.com",
        "/v2/repositories/homeassistant/home-assistant/tags",
        "get",
        aresponses.Response(
            text=fixture("container/page1", False), status=200, headers=HEADERS
        ),
    )
    aresponses.add(
        "registry.hub.docker.com",
        "/v2/repositories/homeassistant/home-assistant/tags/page2",
        "get",
        aresponses.Response(
            text=fixture("container/page2", False), status=200, headers=HEADERS
        ),
    )
    async with aiohttp.ClientSession() as session:
        haversion = HaVersion(
            session=session,
            source=HaVersionSource.CONTAINER,
        )
        await haversion.get_version()
        assert haversion.version == STABLE_VERSION
Exemplo n.º 5
0
async def test_fetch_exception():
    haversion = HaVersion()

    async def mocked_fetch_ImportError(_args):
        raise ImportError

    async def mocked_fetch_ModuleNotFoundError(_args):
        raise ModuleNotFoundError

    async def mocked_fetch_gaierror(_args):
        raise gaierror

    async def mocked_fetch_ClientError(_args):
        raise ClientError

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_ImportError):
        with pytest.raises(HaVersionFetchException):
            await haversion.get_version()

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_ModuleNotFoundError):
        with pytest.raises(HaVersionFetchException):
            await haversion.get_version()

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_gaierror):
        with pytest.raises(HaVersionFetchException):
            await haversion.get_version()

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_ClientError):
        with pytest.raises(HaVersionFetchException):
            await haversion.get_version()
Exemplo n.º 6
0
async def test_parse_exception():
    """Test parse exception."""
    haversion = HaVersion()

    async def mocked_fetch(*_, **__):
        """mocked"""
        pass

    def mocked_parse_KeyError(*_):
        """mocked"""
        raise KeyError

    def mocked_parse_TypeError(*_):
        """mocked"""
        raise TypeError

    with patch("pyhaversion.local.HaVersionLocal.fetch", mocked_fetch):
        with patch("pyhaversion.local.HaVersionLocal.parse",
                   mocked_parse_KeyError):
            with pytest.raises(HaVersionParseException):
                await haversion.get_version()

        with patch("pyhaversion.local.HaVersionLocal.parse",
                   mocked_parse_TypeError):
            with pytest.raises(HaVersionParseException):
                await haversion.get_version()
Exemplo n.º 7
0
async def test_etag(aresponses):
    """Test hassio etag."""
    aresponses.add(
        "version.home-assistant.io",
        "/stable.json",
        "get",
        aresponses.Response(
            text=fixture("supervisor/default", False),
            status=200,
            headers={
                **HEADERS, "Etag": "test"
            },
        ),
    )
    aresponses.add(
        "version.home-assistant.io",
        "/stable.json",
        "get",
        aresponses.Response(status=304, headers=HEADERS),
    )
    async with aiohttp.ClientSession() as session:
        haversion = HaVersion(session=session,
                              source=HaVersionSource.SUPERVISOR)
        await haversion.get_version(etag=haversion.etag)
        assert haversion.version == STABLE_VERSION

        with pytest.raises(HaVersionNotModifiedException):
            await haversion.get_version(etag=haversion.etag)
Exemplo n.º 8
0
async def test_etag(aresponses):
    """Test pypi etag."""
    aresponses.add(
        "pypi.org",
        "/pypi/homeassistant/json",
        "get",
        aresponses.Response(
            text=fixture("pypi/default", False),
            status=200,
            headers={
                **HEADERS, "etag": "test"
            },
        ),
    )
    aresponses.add(
        "pypi.org",
        "/pypi/homeassistant/json",
        "get",
        aresponses.Response(status=304, headers=HEADERS),
    )
    async with aiohttp.ClientSession() as session:
        haversion = HaVersion(session=session, source=HaVersionSource.PYPI)
        await haversion.get_version(etag=haversion.etag)
        assert haversion.version == STABLE_VERSION

        with pytest.raises(HaVersionNotModifiedException):
            await haversion.get_version(etag=haversion.etag)
Exemplo n.º 9
0
async def async_setup_platform(hass,
                               config,
                               async_add_entities,
                               discovery_info=None):
    """Set up the Version sensor platform."""

    beta = config.get(CONF_BETA)
    image = config.get(CONF_IMAGE)
    name = config.get(CONF_NAME)
    source = config.get(CONF_SOURCE)

    session = async_get_clientsession(hass)

    channel = HaVersionChannel.BETA if beta else HaVersionChannel.STABLE

    if source == "pypi":
        haversion = VersionData(
            HaVersion(session, source=HaVersionSource.PYPI, channel=channel))
    elif source in ["hassio", "supervisor"]:
        haversion = VersionData(
            HaVersion(session,
                      source=HaVersionSource.SUPERVISOR,
                      channel=channel,
                      image=image))
    elif source in ["docker", "container"]:
        if image is not None and image != DEFAULT_IMAGE:
            image = f"{image}-homeassistant"
        haversion = VersionData(
            HaVersion(session,
                      source=HaVersionSource.CONTAINER,
                      channel=channel,
                      image=image))
    elif source == "haio":
        haversion = VersionData(HaVersion(session,
                                          source=HaVersionSource.HAIO))
    else:
        haversion = VersionData(
            HaVersion(session, source=HaVersionSource.LOCAL))

    if not name:
        if source == DEFAULT_SOURCE:
            name = DEFAULT_NAME_LOCAL
        else:
            name = DEFAULT_NAME_LATEST

    async_add_entities([VersionSensor(haversion, name)], True)
Exemplo n.º 10
0
async def test_local():
    """Test ha.io/version.json stable."""
    with patch.dict(
        "sys.modules", {"homeassistant.const": MagicMock(__version__=STABLE_VERSION)}
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(session=session, source=HaVersionSource.LOCAL)
            await haversion.get_version()
            assert haversion.version == STABLE_VERSION
Exemplo n.º 11
0
async def test_timeout_exception():
    async def mocked_fetch_TimeoutError(_args):
        raise asyncio.TimeoutError

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_TimeoutError):
        haversion = HaVersion()
        with pytest.raises(HaVersionFetchException):
            await haversion.get_version()
Exemplo n.º 12
0
async def test_stable_version(HaVersion):
    """Test container stable."""
    with patch(
        "pyhaversion.container.HaVersionContainer.data",
        fixture("container/default"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(session=session, source=HaVersionSource.CONTAINER)
            await haversion.get_version()
            assert haversion.version == STABLE_VERSION
Exemplo n.º 13
0
async def test_stable_version(HaVersion):
    """Test docker stable."""
    with patch(
        "pyhaversion.docker.HaVersionDocker.data",
        fixture("docker/default"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(session=session, source=HaVersionSource.DOCKER)
            await haversion.get_version()
            assert haversion.version == STABLE_VERSION
Exemplo n.º 14
0
async def test_not_modified_exception():
    """Test not_modified exception."""
    async def mocked_fetch_not_modified(*_, **__):
        """mocked"""
        raise HaVersionNotModifiedException

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_not_modified):
        haversion = HaVersion()
        with pytest.raises(HaVersionNotModifiedException):
            await haversion.get_version()
Exemplo n.º 15
0
async def test_timeout_exception():
    """Test timeout exception."""
    async def mocked_fetch_TimeoutError(*_, **__):
        """mocked"""
        raise asyncio.TimeoutError

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_TimeoutError):
        haversion = HaVersion()
        with pytest.raises(HaVersionFetchException):
            await haversion.get_version()
Exemplo n.º 16
0
async def test_stable_version(HaVersion):
    """Test pypi stable."""
    with patch(
            "pyhaversion.pypi.HaVersionPypi.data",
            fixture("pypi/default"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(
                session=session,
                source=HaVersionSource.PYPI,
            )
            await haversion.get_version()
            assert haversion.version == STABLE_VERSION
Exemplo n.º 17
0
async def test_stable_version_beta_week(HaVersion):
    """Test docker stable during beta week."""
    with patch(
        "pyhaversion.docker.HaVersionDocker.data",
        fixture("docker/beta_week"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(
                session=session,
                source=HaVersionSource.DOCKER,
            )
            await haversion.get_version()
            assert haversion.version == STABLE_VERSION_BETA_WEEK
Exemplo n.º 18
0
async def test_beta_version(HaVersion):
    """Test docker beta."""
    with patch(
        "pyhaversion.docker.HaVersionDocker.data",
        fixture("docker/default"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(
                session=session,
                source=HaVersionSource.DOCKER,
                channel=HaVersionChannel.DEV,
            )
            await haversion.get_version()
            assert haversion.version == DEV_VERSION
Exemplo n.º 19
0
async def test_beta_version_beta_week(HaVersion):
    """Test container beta during beta week."""
    with patch(
        "pyhaversion.container.HaVersionContainer.data",
        fixture("container/beta_week"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(
                session=session,
                source=HaVersionSource.CONTAINER,
                channel=HaVersionChannel.BETA,
            )
        await haversion.get_version()
        assert haversion.version == BETA_VERSION_BETA_WEEK
Exemplo n.º 20
0
async def test_dev_version(HaVersion):
    """Test container dev."""
    with patch(
        "pyhaversion.container.HaVersionContainer.data",
        fixture("container/default"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(
                session=session,
                source=HaVersionSource.CONTAINER,
                channel=HaVersionChannel.DEV,
            )
            await haversion.get_version()
            assert haversion.version == DEV_VERSION
Exemplo n.º 21
0
async def test_beta_version(HaVersion):
    """Test hassio beta."""
    with patch(
            "pyhaversion.supervised.HaVersionSupervised.data",
            fixture("supervised/default"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(
                session=session,
                source=HaVersionSource.SUPERVISED,
                channel=HaVersionChannel.BETA,
            )
            await haversion.get_version()
            assert haversion.version == STABLE_VERSION
Exemplo n.º 22
0
async def test_beta_version(HaVersion):
    """Test pypi beta."""
    with patch(
            "pyhaversion.pypi.HaVersionPypi.data",
            fixture("pypi/beta"),
    ):
        async with aiohttp.ClientSession() as session:
            haversion = HaVersion(
                session=session,
                source=HaVersionSource.PYPI,
                channel=HaVersionChannel.BETA,
            )
            await haversion.get_version()
            assert haversion.version == BETA_VERSION
Exemplo n.º 23
0
async def test_haio(aresponses):
    """Test ha.io/version.json stable."""
    aresponses.add(
        "www.home-assistant.io",
        "/version.json",
        "get",
        aresponses.Response(
            text=fixture("haio/default", False), status=200, headers=HEADERS
        ),
    )
    async with aiohttp.ClientSession() as session:
        haversion = HaVersion(session=session, source=HaVersionSource.HAIO)
        await haversion.get_version()
        assert haversion.version == STABLE_VERSION
Exemplo n.º 24
0
async def test_stable_version(aresponses):
    """Test hassio stable."""
    aresponses.add(
        "version.home-assistant.io",
        "/stable.json",
        "get",
        aresponses.Response(text=fixture("supervisor/default", False),
                            status=200,
                            headers=HEADERS),
    )
    async with aiohttp.ClientSession() as session:
        haversion = HaVersion(session=session,
                              source=HaVersionSource.SUPERVISOR)
        await haversion.get_version()
        assert haversion.version == STABLE_VERSION
Exemplo n.º 25
0
async def test_stable_version_beta_week(aresponses):
    """Test pypi stable during beta week."""
    aresponses.add(
        "pypi.org",
        "/pypi/homeassistant/json",
        "get",
        aresponses.Response(text=fixture("pypi/beta", False),
                            status=200,
                            headers=HEADERS),
    )
    async with aiohttp.ClientSession() as session:
        haversion = HaVersion(
            session=session,
            source=HaVersionSource.PYPI,
        )
        await haversion.get_version()
        assert haversion.version == STABLE_VERSION_BETA_WEEK
Exemplo n.º 26
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up the version integration from a config entry."""
    coordinator = VersionDataUpdateCoordinator(
        hass=hass,
        api=HaVersion(
            session=async_get_clientsession(hass),
            source=entry.data[CONF_SOURCE],
            image=entry.data[CONF_IMAGE],
            board=BOARD_MAP[entry.data[CONF_BOARD]],
            channel=entry.data[CONF_CHANNEL].lower(),
        ),
    )
    await coordinator.async_config_entry_first_refresh()

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
Exemplo n.º 27
0
async def test_fetch_exception():
    """Test fetch exception."""
    haversion = HaVersion()

    async def mocked_fetch_gaierror(*_, **__):
        """mocked"""
        raise gaierror

    async def mocked_fetch_ClientError(*_, **__):
        """mocked"""
        raise ClientError

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_gaierror):
        with pytest.raises(HaVersionFetchException):
            await haversion.get_version()

    with patch("pyhaversion.local.HaVersionLocal.fetch",
               mocked_fetch_ClientError):
        with pytest.raises(HaVersionFetchException):
            await haversion.get_version()
Exemplo n.º 28
0
async def test_parse_exception():
    haversion = HaVersion()

    async def mocked_fetch(_args):
        pass

    def mocked_parse_KeyError(_args):
        raise KeyError

    def mocked_parse_TypeError(_args):
        raise TypeError

    with patch("pyhaversion.local.HaVersionLocal.fetch", mocked_fetch):
        with patch("pyhaversion.local.HaVersionLocal.parse",
                   mocked_parse_KeyError):
            with pytest.raises(HaVersionParseException):
                await haversion.get_version()

        with patch("pyhaversion.local.HaVersionLocal.parse",
                   mocked_parse_TypeError):
            with pytest.raises(HaVersionParseException):
                await haversion.get_version()
Exemplo n.º 29
0
async def test_input_exception(HaVersion):
    with pytest.raises(HaVersionInputException):
        HaVersion(source=HaVersionSource.PYPI)
Exemplo n.º 30
0
    if (source == HaVersionSource.CONTAINER and image is not None
            and image != DEFAULT_IMAGE):
        image = f"{image}-homeassistant"

    if not (name := config.get(CONF_NAME)):
        if source == HaVersionSource.LOCAL:
            name = DEFAULT_NAME_LOCAL
        else:
            name = DEFAULT_NAME_LATEST

    async_add_entities(
        [
            VersionSensor(
                VersionData(
                    HaVersion(session=session,
                              source=source,
                              image=image,
                              channel=channel)),
                SensorEntityDescription(key=source, name=name),
            )
        ],
        True,
    )


class VersionData:
    """Get the latest data and update the states."""
    def __init__(self, api: HaVersion) -> None:
        """Initialize the data object."""
        self.api = api

    @Throttle(TIME_BETWEEN_UPDATES)