Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Move to async
Browse files Browse the repository at this point in the history
  • Loading branch information
rccoleman committed Nov 30, 2020
1 parent eda8772 commit 07e02f3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
40 changes: 23 additions & 17 deletions custom_components/lamarzocco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"""
Expand All @@ -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)
await self.client.post(self.status_endpoint, json=command)
2 changes: 1 addition & 1 deletion custom_components/lamarzocco/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "La Marzocco",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/lamarzocco",
"requirements": [],
"requirements": ["authlib"],
"ssdp": [],
"zeroconf": [],
"homekit": {},
Expand Down
12 changes: 6 additions & 6 deletions custom_components/lamarzocco/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 07e02f3

Please sign in to comment.