From 07e02f3da16c94228cba207512716912814b3d47 Mon Sep 17 00:00:00 2001 From: Rob Coleman Date: Mon, 30 Nov 2020 05:34:00 +0000 Subject: [PATCH] Move to async --- custom_components/lamarzocco/__init__.py | 40 +++++++++++++--------- custom_components/lamarzocco/manifest.json | 2 +- custom_components/lamarzocco/switch.py | 12 +++---- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/custom_components/lamarzocco/__init__.py b/custom_components/lamarzocco/__init__.py index 3050ccc..b9a4408 100644 --- a/custom_components/lamarzocco/__init__.py +++ b/custom_components/lamarzocco/__init__.py @@ -4,6 +4,7 @@ # from authlib.integrations.requests_client import OAuth2Session from requests import Response from requests_oauthlib import OAuth2Session +from authlib.integrations.httpx_client import AsyncOAuth2Client from homeassistant.config_entries import ConfigEntry @@ -55,7 +56,7 @@ async def async_setup(hass: HomeAssistant, config: dict): async def async_setup_entry(hass, config_entry): """Set up La Marzocco as config entry.""" coordinator = LaMarzoccoDataUpdateCoordinator(hass, config_entry) - await hass.async_add_executor_job(coordinator.init_data) + await coordinator.init_data() await coordinator.async_refresh() if not coordinator.last_update_success: @@ -103,9 +104,9 @@ def __init__(self, hass, config_entry): update_method=self.async_update_data, ) - def init_data(self): + async def init_data(self): """Initialize the UpdateCoordinator object""" - self._device.init_data() + await self._device.init_data() async def async_update_data(self): """Fetch data""" @@ -126,45 +127,50 @@ def __init__(self, hass, config): self.client = None self.is_on = False - def init_data(self): + async def init_data(self): """Machine data inialization""" serial_number = self._config[CONF_SERIAL_NUMBER] self.config_endpoint = f"{GW_URL}/{serial_number}/configuration" self.status_endpoint = f"{GW_URL}/{serial_number}/status" - self.client = OAuth2Session( - client=oauthlib.oauth2.LegacyApplicationClient(self._config[CONF_CLIENT_ID]) + token_endpoint = "https://cms.lamarzocco.io/oauth/v2/token" + client_id = self._config[CONF_CLIENT_ID] + client_secret = self._config[CONF_CLIENT_SECRET] + + self.client = AsyncOAuth2Client( + client_id=client_id, + client_secret=client_secret, + token_endpoint=token_endpoint, ) - self.client.fetch_token( - "https://cms.lamarzocco.io/oauth/v2/token", - client_secret=self._config[CONF_CLIENT_SECRET], + headers = {"client_id": client_id, "client_secret": client_secret} + + await self.client.fetch_token( + url=token_endpoint, username=self._config[CONF_USERNAME], password=self._config[CONF_PASSWORD], + headers=headers, ) async def fetch_data(self): """Fetch data from API - (current weather and forecast).""" - await self.hass.async_add_executor_job(self._fetch_data) - return self - - def _fetch_data(self): _LOGGER.debug("Fetching data") - current_status = self.client.get(self.status_endpoint) + current_status = await self.client.get(self.status_endpoint) if current_status is not None: _LOGGER.debug(current_status.json()) data = current_status.json() if data is not None: self.is_on = data[DATA_TAG][MACHINE_STATUS] == STATUS_ON - current_data = self.client.get(self.config_endpoint) + current_data = await self.client.get(self.config_endpoint) if current_data is not None: _LOGGER.debug(current_data.json()) self.current_data = current_data.json().get(DATA_TAG) _LOGGER.debug("Device is {}".format("On" if self.is_on else "Off")) _LOGGER.debug("Data is {}".format(self.current_data)) + return self - def power(self, power): + async def power(self, power): command = COMMAND_ON if power else COMMAND_STANDBY - self.client.post(self.status_endpoint, json=command) \ No newline at end of file + await self.client.post(self.status_endpoint, json=command) \ No newline at end of file diff --git a/custom_components/lamarzocco/manifest.json b/custom_components/lamarzocco/manifest.json index aada53d..1cc83de 100644 --- a/custom_components/lamarzocco/manifest.json +++ b/custom_components/lamarzocco/manifest.json @@ -3,7 +3,7 @@ "name": "La Marzocco", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/lamarzocco", - "requirements": [], + "requirements": ["authlib"], "ssdp": [], "zeroconf": [], "homekit": {}, diff --git a/custom_components/lamarzocco/switch.py b/custom_components/lamarzocco/switch.py index 0e7ab01..0b379a8 100644 --- a/custom_components/lamarzocco/switch.py +++ b/custom_components/lamarzocco/switch.py @@ -33,17 +33,17 @@ def __init__(self, coordinator, config, is_metric): self._temp_state = None self.is_metric = is_metric - def turn_on(self, **kwargs) -> None: + async def async_turn_on(self, **kwargs) -> None: """Turn device on.""" - self.coordinator.data.power(True) + await self.coordinator.data.power(True) self._temp_state = True - self.schedule_update_ha_state(force_refresh=False) + self.async_schedule_update_ha_state(force_refresh=False) - def turn_off(self, **kwargs) -> None: + async def async_turn_off(self, **kwargs) -> None: """Turn device off.""" - self.coordinator.data.power(False) + await self.coordinator.data.power(False) self._temp_state = False - self.schedule_update_ha_state(force_refresh=False) + self.async_schedule_update_ha_state(force_refresh=False) @callback def _handle_coordinator_update(self) -> None: