async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Wallbox from a config entry.""" wallbox = Wallbox( entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], ) try: await hass.async_add_executor_job(wallbox.authenticate) except ConnectionError as exception: _LOGGER.error("Unable to fetch data from Wallbox Switch. %s", exception) return False hass.data.setdefault(DOMAIN, {CONF_CONNECTIONS: {}}) hass.data[DOMAIN][CONF_CONNECTIONS][entry.entry_id] = wallbox for platform in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, platform) ) return True
def authenticate(self) -> bool: """Authenticate using Wallbox API.""" try: wallbox = Wallbox(self._username, self._password) 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
async def validate_input(hass: core.HomeAssistant, data): """Validate the user input allows to connect. Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ wallbox = Wallbox(data["username"], data["password"]) wallbox_coordinator = WallboxCoordinator(data["station"], wallbox, hass) await wallbox_coordinator.async_validate_input() # Return info that you want to store in the config entry. return {"title": "Wallbox Portal"}
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
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), )
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Wallbox from a config entry.""" wallbox = Wallbox(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]) wallbox_coordinator = WallboxCoordinator( entry.data[CONF_STATION], wallbox, hass, ) try: await wallbox_coordinator.async_validate_input() except InvalidAuth as ex: raise ConfigEntryAuthFailed from ex await wallbox_coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {})[entry.entry_id] = wallbox_coordinator hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Wallbox from a config entry.""" wallbox = Wallbox(entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]) wallbox_coordinator = WallboxCoordinator( entry.data[CONF_STATION], wallbox, hass, ) await wallbox_coordinator.async_validate_input() await wallbox_coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {CONF_CONNECTIONS: {}}) hass.data[DOMAIN][CONF_CONNECTIONS][entry.entry_id] = wallbox_coordinator for platform in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, platform)) return True