Exemplo n.º 1
0
async def test_beta_version_beta_week(HaVersion):
    """Test docker beta 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,
                channel=HaVersionChannel.BETA,
            )
        await haversion.get_version()
        assert haversion.version == BETA_VERSION_BETA_WEEK
Exemplo n.º 2
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
            assert haversion.source == HaVersionSource.PYPI
Exemplo n.º 3
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.º 4
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.º 5
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.º 6
0
async def test_beta_version(HaVersion):
    """Test container beta."""
    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
Exemplo n.º 7
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.º 8
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.º 9
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.º 10
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.º 11
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.º 12
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.º 13
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.º 14
0
async def test_input_exception(HaVersion):
    with pytest.raises(HaVersionInputException):
        HaVersion(source=HaVersionSource.PYPI)
Exemplo n.º 15
0
async def test_input_exception(HaVersion):
    """Test input exception."""
    with pytest.raises(HaVersionInputException):
        HaVersion(source=HaVersionSource.SUPERVISOR)
Exemplo n.º 16
0
async def update_data(hass):
    """Update data."""
    throttle = hass.data[DOMAIN_DATA]["throttle"]
    if throttle.throttle:
        return

    integrations = []
    changes = hass.data[DOMAIN_DATA]["potential"]["changes"]

    session = async_get_clientsession(hass)
    webclient = WebClient(session)
    localversion = HaVersion(session, source=HaVersionSource.LOCAL)
    pypiversion = HaVersion(session, source=HaVersionSource.PYPI)
    throttle.set_last_run()

    try:
        await localversion.get_version()
        currentversion = AwesomeVersion(localversion.version)

        await pypiversion.get_version()
        remoteversion = AwesomeVersion(pypiversion.version)
    except Exception:  # pylint: disable=broad-except
        _LOGGER.warning("Could not get version data.")
        return

    if currentversion >= remoteversion:
        _LOGGER.debug(
            "Current version is %s and remote version is %s skipping update",
            currentversion,
            remoteversion,
        )
        return

    for integration in hass.config.components or []:
        if "." in integration:
            integration = integration.split(".")[0]
        if integration not in integrations:
            integrations.append(integration)

    _LOGGER.debug("Loaded integrations - %s", integrations)

    c_split = [int(x) for x in currentversion.split(".")]
    r_split = [int(x) for x in remoteversion.split(".")]

    request_versions = []
    if c_split[0] < r_split[0]:
        for version in range(c_split[1] + 1, 13):
            request_versions.append(f"{c_split[0]}.{version}")
        for version in range(1, r_split[1] + 1):
            request_versions.append(f"{r_split[0]}.{version}")
    else:
        for version in range(c_split[1] + 1, r_split[1] + 1):
            request_versions.append(f"{r_split[0]}.{version}")

    request_versions = [x for x in request_versions if x >= MINIMUM_VERSION]
    if len(request_versions) == 0:
        _LOGGER.debug("no valid versions")
        return

    if hass.data[DOMAIN_DATA]["integrations"] != integrations:
        hass.data[DOMAIN_DATA]["potential"]["versions"] = set()
        changes = []
    hass.data[DOMAIN_DATA]["integrations"] = integrations

    for version in request_versions:
        if version in hass.data[DOMAIN_DATA]["potential"]["versions"]:
            _LOGGER.debug("Allready have information for %s", version)
            continue
        try:
            _LOGGER.debug("Checking breaking changes for %s", version)
            request = await webclient.async_get_json(URL.format(version))

            for change in request or []:
                if change["integration"] in integrations:
                    data = {
                        "title": change["title"],
                        "integration": change["integration"],
                        "description": change["description"],
                    }
                    changes.append(data)

            if version not in hass.data[DOMAIN_DATA]["potential"]["versions"]:
                hass.data[DOMAIN_DATA]["potential"]["versions"].add(version)

        except Exception as error:  # pylint: disable=broad-except
            _LOGGER.error("Could not update data - %s", error)

    hass.data[DOMAIN_DATA]["potential"]["changes"] = changes
Exemplo n.º 17
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)