Example #1
0
    def update(self):
        """Get the latest data and updates the states."""

        _LOGGER.debug("Update data for %s", self._friendly_name)

        if self._country_code is not None:
            self._data = CO2Signal.get_latest_carbon_intensity(
                self._token, country_code=self._country_code)
        else:
            self._data = CO2Signal.get_latest_carbon_intensity(
                self._token,
                latitude=self._latitude,
                longitude=self._longitude)

        self._data = round(self._data, 2)
Example #2
0
    def update(self):
        """Get the latest data and updates the states."""
        import CO2Signal

        _LOGGER.debug("Update data for %s", self._friendly_name)

        if self._country_code is not None:
            self._data = CO2Signal.get_latest_carbon_intensity(
                self._token, country_code=self._country_code)
        else:
            self._data = CO2Signal.get_latest_carbon_intensity(
                self._token,
                latitude=self._latitude, longitude=self._longitude)

        self._data = round(self._data, 2)
    def update(self):
        """Get the latest data and updates the states."""
        import CO2Signal

        _LOGGER.debug("Update data for %s", self._friendly_name)

        if self._location_type == 'country_code':
            self._data = CO2Signal.get_latest_carbon_intensity(
                self._token, country_code=self._country_code)
        elif self._location_type == 'coordinates':
            self._data = CO2Signal.get_latest_carbon_intensity(
                self._token,
                latitude=self._latitude,
                longitude=self._longitude)
        else:
            raise ValueError("Unknown location type: {location_type}".format(
                location_type=self._location_type))
Example #4
0
def get_data(hass: HomeAssistant, config: dict) -> CO2SignalResponse:
    """Get data from the API."""
    if CONF_COUNTRY_CODE in config:
        latitude = None
        longitude = None
    else:
        latitude = config.get(CONF_LATITUDE, hass.config.latitude)
        longitude = config.get(CONF_LONGITUDE, hass.config.longitude)

    try:
        data = CO2Signal.get_latest(
            config[CONF_API_KEY],
            config.get(CONF_COUNTRY_CODE),
            latitude,
            longitude,
            wait=False,
        )

    except ValueError as err:
        err_str = str(err)

        if "Invalid authentication credentials" in err_str:
            raise InvalidAuth from err
        if "API rate limit exceeded." in err_str:
            raise APIRatelimitExceeded from err

        _LOGGER.exception("Unexpected exception")
        raise UnknownError from err
    except Exception as err:
        _LOGGER.exception("Unexpected exception")
        raise UnknownError from err

    else:
        if "error" in data:
            raise UnknownError(data["error"])

        if data.get("status") != "ok":
            _LOGGER.exception("Unexpected response: %s", data)
            raise UnknownError

    return cast(CO2SignalResponse, data)