示例#1
0
    def get_data(self) -> bool:
        """Get new sensor data for Wallbox component."""

        try:
            wallbox = Wallbox(self._username, self._password)
            wallbox.authenticate()
            wallbox.getChargerStatus(self._station)
            return True
        except requests.exceptions.HTTPError as wallbox_connection_error:
            if wallbox_connection_error.response.status_code == "403":
                raise InvalidAuth from wallbox_connection_error
            raise ConnectionError from wallbox_connection_error
示例#2
0
文件: __init__.py 项目: tpaulus/core
class WallboxHub:
    """Wallbox Hub class."""
    def __init__(self, station, username, password, hass):
        """Initialize."""
        self._station = station
        self._username = username
        self._password = password
        self._wallbox = Wallbox(self._username, self._password)
        self._hass = hass
        self._coordinator = DataUpdateCoordinator(
            hass,
            _LOGGER,
            # Name of the data. For logging purposes.
            name="wallbox",
            update_method=self.async_get_data,
            # Polling interval. Will only be polled if there are subscribers.
            update_interval=timedelta(seconds=UPDATE_INTERVAL),
        )

    def _authenticate(self):
        """Authenticate using Wallbox API."""
        try:
            self._wallbox.authenticate()
            return True
        except requests.exceptions.HTTPError as wallbox_connection_error:
            if wallbox_connection_error.response.status_code == 403:
                raise InvalidAuth from wallbox_connection_error
            raise ConnectionError from wallbox_connection_error

    def _get_data(self):
        """Get new sensor data for Wallbox component."""
        try:
            self._authenticate()
            data = self._wallbox.getChargerStatus(self._station)

            filtered_data = {
                k: data[k]
                for k in CONF_SENSOR_TYPES if k in data
            }

            for key, value in filtered_data.items():
                sensor_round = CONF_SENSOR_TYPES[key][CONF_ROUND]
                if sensor_round:
                    try:
                        filtered_data[key] = round(value, sensor_round)
                    except TypeError:
                        _LOGGER.debug("Cannot format %s", key)

            return filtered_data
        except requests.exceptions.HTTPError as wallbox_connection_error:
            raise ConnectionError from wallbox_connection_error

    async def async_coordinator_first_refresh(self):
        """Refresh coordinator for the first time."""
        await self._coordinator.async_config_entry_first_refresh()

    async def async_authenticate(self) -> bool:
        """Authenticate using Wallbox API."""
        return await self._hass.async_add_executor_job(self._authenticate)

    async def async_get_data(self) -> bool:
        """Get new sensor data for Wallbox component."""
        data = await self._hass.async_add_executor_job(self._get_data)
        return data

    @property
    def coordinator(self):
        """Return the coordinator."""
        return self._coordinator