Exemplo n.º 1
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)

    if beta:
        branch = "beta"
    else:
        branch = "stable"

    if source == "pypi":
        haversion = VersionData(PyPiVersion(hass.loop, session, branch))
    elif source == "hassio":
        haversion = VersionData(HassioVersion(hass.loop, session, branch, image))
    elif source == "docker":
        haversion = VersionData(DockerVersion(hass.loop, session, branch, image))
    elif source == "haio":
        haversion = VersionData(HaIoVersion(hass.loop, session))
    else:
        haversion = VersionData(LocalVersion(hass.loop, session))

    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.º 2
0
async def update_data(hass):
    """Update data."""
    if len(hass.data[DOMAIN_DATA]["components"]) == 1:
        return
    from pyhaversion import LocalVersion, PyPiVersion

    session = async_get_clientsession(hass)
    localversion = LocalVersion(hass.loop, session)
    pypiversion = PyPiVersion(hass.loop, session)

    await localversion.get_version()
    currentversion = localversion.version.split(".")[1]

    await pypiversion.get_version()
    remoteversion = pypiversion.version.split(".")[1]

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

    versions = []

    for platform in hass.data[DOMAIN_DATA]["components"]:
        if "homeassistant.components." in platform:
            name = platform.split("homeassistant.components.")[1]
            if "." in name:
                name = name.split(".")[0]
            if name not in hass.data[DOMAIN_DATA]["components"]:
                hass.data[DOMAIN_DATA]["components"].append(name)
    _LOGGER.debug("Loaded components - %s", hass.data[DOMAIN_DATA]["components"])

    try:
        _LOGGER.debug("Running update")

        for version in range(int(currentversion) + 1, int(remoteversion) + 1):
            versions.append(version)
            request = requests.get(URL.format(version))
            jsondata = request.json()
            _LOGGER.debug(jsondata)
            for platform in jsondata:
                _LOGGER.debug(platform["component"])
                if platform["component"] is None or platform["component"] is "None":
                    platform["component"] = "homeassistant"
                if platform["component"] in hass.data[DOMAIN_DATA]["components"]:
                    data = {
                        "component": platform["component"],
                        "prlink": platform["prlink"],
                        "doclink": platform["doclink"],
                        "description": platform["description"],
                    }
                    hass.data[DOMAIN_DATA]["potential"][platform["pull_request"]] = data
        hass.data[DOMAIN_DATA]["potential"]["versions"] = versions
    except Exception as error:  # pylint: disable=broad-except
        _LOGGER.error("Could not update data - %s", error)
Exemplo n.º 3
0
async def update_data(hass):
    """Update data."""
    throttle = hass.data[DOMAIN_DATA]["throttle"]
    if throttle.throttle:
        return

    versions = set()
    integrations = set()
    covered = set()
    changes = []

    hass.data[DOMAIN_DATA]["potential"] = {}

    session = async_get_clientsession(hass)
    webclient = WebClient(session)
    localversion = LocalVersion(hass.loop, session)
    pypiversion = PyPiVersion(hass.loop, session)
    throttle.set_last_run()

    try:
        await localversion.get_version()
        currentversion = localversion.version.split(".")[1]

        await pypiversion.get_version()
        remoteversion = pypiversion.version.split(".")[1]
    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]
        integrations.add(integration)

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

    try:
        _LOGGER.debug("Running update")
        request = await webclient.async_get_json(
            URL.format(currentversion, remoteversion))
        _LOGGER.debug(request)

        for change in request or []:
            if change["pull"] in covered:
                continue

            if change["integration"] in integrations:
                data = {
                    "title": change["title"],
                    "integration": change["integration"],
                    "prlink": change["prlink"],
                    "doclink": change["doclink"],
                    "description": change["description"],
                }

                changes.append(data)
                covered.add(change["pull"])

                if change["homeassistant"] not in versions:
                    versions.add(change["homeassistant"])

        hass.data[DOMAIN_DATA]["potential"]["changes"] = changes
        hass.data[DOMAIN_DATA]["potential"]["versions"] = versions
    except Exception as error:  # pylint: disable=broad-except
        _LOGGER.error("Could not update data - %s", error)
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 = LocalVersion(hass.loop, session)
    pypiversion = PyPiVersion(hass.loop, session)
    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