Skip to content

Commit

Permalink
Performance improvements and better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dcmeglio committed Jan 22, 2023
1 parent 7b8bb7e commit ead2b80
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 1 addition & 3 deletions custom_components/waste_management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions custom_components/waste_management/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions custom_components/waste_management/manifest.json
Original file line number Diff line number Diff line change
@@ -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": [],
Expand Down
21 changes: 17 additions & 4 deletions custom_components/waste_management/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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])
Expand Down Expand Up @@ -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

0 comments on commit ead2b80

Please # to comment.