From ead2b8061f268cd33fefb54e9f2dfffa1fb54462 Mon Sep 17 00:00:00 2001 From: Dominick Meglio Date: Sun, 22 Jan 2023 17:45:27 -0500 Subject: [PATCH] Performance improvements and better error handling --- .../waste_management/__init__.py | 4 +--- .../waste_management/config_flow.py | 7 ++++--- .../waste_management/manifest.json | 4 ++-- custom_components/waste_management/sensor.py | 21 +++++++++++++++---- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/custom_components/waste_management/__init__.py b/custom_components/waste_management/__init__.py index 6533f1d..4852dee 100644 --- a/custom_components/waste_management/__init__.py +++ b/custom_components/waste_management/__init__.py @@ -14,9 +14,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up Waste Management Pickup from a config entry.""" hass.data.setdefault(DOMAIN, {}) - # TODO 1. Create API instance - # TODO 2. Validate the API connection (and authentication) - # TODO 3. Store an API object for your platforms to access + hass.data[DOMAIN][entry.entry_id] = entry.data await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/custom_components/waste_management/config_flow.py b/custom_components/waste_management/config_flow.py index 6534a39..de86dce 100644 --- a/custom_components/waste_management/config_flow.py +++ b/custom_components/waste_management/config_flow.py @@ -12,6 +12,7 @@ from homeassistant.data_entry_flow import FlowResult from homeassistant.exceptions import HomeAssistantError import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.httpx_client import get_async_client from waste_management import WMClient @@ -41,13 +42,13 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ - hub = WMClient(data["username"], data["password"]) + hub = WMClient(data["username"], data["password"], get_async_client(hass)) try: await hub.async_authenticate() await hub.async_okta_authorize() - except Exception: - raise InvalidAuth + except Exception as ex: + raise InvalidAuth from ex return hub diff --git a/custom_components/waste_management/manifest.json b/custom_components/waste_management/manifest.json index 373986c..a96da8e 100644 --- a/custom_components/waste_management/manifest.json +++ b/custom_components/waste_management/manifest.json @@ -1,12 +1,12 @@ { - "version": "0.1.4", + "version": "0.1.5", "domain": "waste_management", "name": "Waste Management", "config_flow": true, "documentation": "https://github.com/dcmeglio/homeassistant-waste_management/blob/master/README", "issue_tracker": "https://github.com/dcmeglio/homeassistant-waste_management/issues", "requirements": [ - "waste-management==3.0.1" + "waste-management==3.1.0" ], "ssdp": [], "zeroconf": [], diff --git a/custom_components/waste_management/sensor.py b/custom_components/waste_management/sensor.py index 1c628d0..3696150 100644 --- a/custom_components/waste_management/sensor.py +++ b/custom_components/waste_management/sensor.py @@ -8,6 +8,7 @@ from homeassistant.exceptions import PlatformNotReady import homeassistant.helpers.event from homeassistant.core import HomeAssistant +from homeassistant.helpers.httpx_client import get_async_client from waste_management import WMClient @@ -19,7 +20,11 @@ async def async_setup_entry(hass: HomeAssistant, config, add_entities): config_data = config.data entities = [] try: - client = WMClient(config_data[CONF_USERNAME], config_data[CONF_PASSWORD]) + client = WMClient( + config_data[CONF_USERNAME], + config_data[CONF_PASSWORD], + get_async_client(hass), + ) await client.async_authenticate() await client.async_okta_authorize() wm_services = await client.async_get_services(config_data[CONF_ACCOUNT]) @@ -64,14 +69,22 @@ async def timer_callback(self, datetime): async def async_update(self) -> None: client = WMClient(self.username, self.password) - await client.async_authenticate() + pickup = None + try: + await client.async_authenticate() - await client.async_okta_authorize() + await client.async_okta_authorize() - pickup = await client.async_get_service_pickup(self.account_id, self.service_id) + pickup = await client.async_get_service_pickup( + self.account_id, self.service_id + ) + except Exception: + self._attr_available = False + return today = datetime.date.today() proposed_pickup = pickup[0].astimezone() if proposed_pickup.date() < today and len(pickup) > 1: proposed_pickup = pickup[1].astimezone() self._attr_native_value = proposed_pickup + self._attr_available = True