Bump pyaftership to 21.11.0 (#64294)

This commit is contained in:
Joakim Sørensen 2022-01-17 14:14:57 +01:00 committed by GitHub
parent d84beefd4e
commit b45f3b97c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 35 deletions

View File

@ -2,7 +2,9 @@
"domain": "aftership", "domain": "aftership",
"name": "AfterShip", "name": "AfterShip",
"documentation": "https://www.home-assistant.io/integrations/aftership", "documentation": "https://www.home-assistant.io/integrations/aftership",
"requirements": ["pyaftership==0.1.2"], "requirements": [
"pyaftership==21.11.0"
],
"codeowners": [], "codeowners": [],
"iot_class": "cloud_polling" "iot_class": "cloud_polling"
} }

View File

@ -1,11 +1,10 @@
"""Support for non-delivered packages recorded in AfterShip.""" """Support for non-delivered packages recorded in AfterShip."""
from __future__ import annotations from __future__ import annotations
from http import HTTPStatus
import logging import logging
from typing import Any, Final from typing import Any, Final
from pyaftership.tracker import Tracking from pyaftership import AfterShip, AfterShipException
import voluptuous as vol import voluptuous as vol
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
@ -60,27 +59,23 @@ async def async_setup_platform(
name = config[CONF_NAME] name = config[CONF_NAME]
session = async_get_clientsession(hass) session = async_get_clientsession(hass)
aftership = Tracking(hass.loop, session, apikey) aftership = AfterShip(api_key=apikey, session=session)
await aftership.get_trackings() try:
await aftership.trackings.list()
if not aftership.meta or aftership.meta["code"] != HTTPStatus.OK: except AfterShipException as err:
_LOGGER.error( _LOGGER.error("No tracking data found. Check API key is correct: %s", err)
"No tracking data found. Check API key is correct: %s", aftership.meta
)
return return
instance = AfterShipSensor(aftership, name) async_add_entities([AfterShipSensor(aftership, name)], True)
async_add_entities([instance], True)
async def handle_add_tracking(call: ServiceCall) -> None: async def handle_add_tracking(call: ServiceCall) -> None:
"""Call when a user adds a new Aftership tracking from Home Assistant.""" """Call when a user adds a new Aftership tracking from Home Assistant."""
title = call.data.get(CONF_TITLE) await aftership.trackings.add(
slug = call.data.get(CONF_SLUG) tracking_number=call.data[CONF_TRACKING_NUMBER],
tracking_number = call.data[CONF_TRACKING_NUMBER] title=call.data.get(CONF_TITLE),
slug=call.data.get(CONF_SLUG),
await aftership.add_package_tracking(tracking_number, title, slug) )
async_dispatcher_send(hass, UPDATE_TOPIC) async_dispatcher_send(hass, UPDATE_TOPIC)
hass.services.async_register( hass.services.async_register(
@ -92,10 +87,10 @@ async def async_setup_platform(
async def handle_remove_tracking(call: ServiceCall) -> None: async def handle_remove_tracking(call: ServiceCall) -> None:
"""Call when a user removes an Aftership tracking from Home Assistant.""" """Call when a user removes an Aftership tracking from Home Assistant."""
slug = call.data[CONF_SLUG] await aftership.trackings.remove(
tracking_number = call.data[CONF_TRACKING_NUMBER] tracking_number=call.data[CONF_TRACKING_NUMBER],
slug=call.data[CONF_SLUG],
await aftership.remove_package_tracking(slug, tracking_number) )
async_dispatcher_send(hass, UPDATE_TOPIC) async_dispatcher_send(hass, UPDATE_TOPIC)
hass.services.async_register( hass.services.async_register(
@ -113,7 +108,7 @@ class AfterShipSensor(SensorEntity):
_attr_native_unit_of_measurement: str = "packages" _attr_native_unit_of_measurement: str = "packages"
_attr_icon: str = ICON _attr_icon: str = ICON
def __init__(self, aftership: Tracking, name: str) -> None: def __init__(self, aftership: AfterShip, name: str) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self._attributes: dict[str, Any] = {} self._attributes: dict[str, Any] = {}
self._state: int | None = None self._state: int | None = None
@ -146,15 +141,10 @@ class AfterShipSensor(SensorEntity):
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self, **kwargs: Any) -> None: async def async_update(self, **kwargs: Any) -> None:
"""Get the latest data from the AfterShip API.""" """Get the latest data from the AfterShip API."""
await self.aftership.get_trackings() try:
trackings = await self.aftership.trackings.list()
if not self.aftership.meta: except AfterShipException as err:
_LOGGER.error("Unknown errors when querying") _LOGGER.error("Errors when querying AfterShip - %s", err)
return
if self.aftership.meta["code"] != HTTPStatus.OK:
_LOGGER.error(
"Errors when querying AfterShip. %s", str(self.aftership.meta)
)
return return
status_to_ignore = {"delivered"} status_to_ignore = {"delivered"}
@ -162,7 +152,7 @@ class AfterShipSensor(SensorEntity):
trackings = [] trackings = []
not_delivered_count = 0 not_delivered_count = 0
for track in self.aftership.trackings["trackings"]: for track in trackings:
status = track["tag"].lower() status = track["tag"].lower()
name = ( name = (
track["tracking_number"] if track["title"] is None else track["title"] track["tracking_number"] if track["title"] is None else track["title"]

View File

@ -1374,7 +1374,7 @@ pyads==3.2.2
pyaehw4a1==0.3.9 pyaehw4a1==0.3.9
# homeassistant.components.aftership # homeassistant.components.aftership
pyaftership==0.1.2 pyaftership==21.11.0
# homeassistant.components.airnow # homeassistant.components.airnow
pyairnow==1.1.0 pyairnow==1.1.0