Beispiel #1
0
async def detect_couriers_example():
    """Detect couriers for tracking number."""
    async with aiohttp.ClientSession() as session:
        pyaftership = Tracking(LOOP, session, API_KEY)
        tracking_number = "1Z9999999999999999"
        couriers = await pyaftership.detect_couriers_for_tracking_number(
            tracking_number)
        print("Possible couriers:", couriers)
Beispiel #2
0
async def async_setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    async_add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the AfterShip sensor platform."""
    apikey = config[CONF_API_KEY]
    name = config[CONF_NAME]

    session = async_get_clientsession(hass)
    aftership = Tracking(hass.loop, session, apikey)

    await aftership.get_trackings()

    if not aftership.meta or aftership.meta["code"] != HTTP_OK:
        _LOGGER.error(
            "No tracking data found. Check API key is correct: %s", aftership.meta
        )
        return

    instance = AfterShipSensor(aftership, name)

    async_add_entities([instance], True)

    async def handle_add_tracking(call: ServiceCall) -> None:
        """Call when a user adds a new Aftership tracking from Home Assistant."""
        title = call.data.get(CONF_TITLE)
        slug = call.data.get(CONF_SLUG)
        tracking_number = call.data[CONF_TRACKING_NUMBER]

        await aftership.add_package_tracking(tracking_number, title, slug)
        async_dispatcher_send(hass, UPDATE_TOPIC)

    hass.services.async_register(
        DOMAIN,
        SERVICE_ADD_TRACKING,
        handle_add_tracking,
        schema=ADD_TRACKING_SERVICE_SCHEMA,
    )

    async def handle_remove_tracking(call: ServiceCall) -> None:
        """Call when a user removes an Aftership tracking from Home Assistant."""
        slug = call.data[CONF_SLUG]
        tracking_number = call.data[CONF_TRACKING_NUMBER]

        await aftership.remove_package_tracking(slug, tracking_number)
        async_dispatcher_send(hass, UPDATE_TOPIC)

    hass.services.async_register(
        DOMAIN,
        SERVICE_REMOVE_TRACKING,
        handle_remove_tracking,
        schema=REMOVE_TRACKING_SERVICE_SCHEMA,
    )
Beispiel #3
0
async def async_setup_platform(
        hass, config, async_add_entities, discovery_info=None):
    """Set up the AfterShip sensor platform."""
    from pyaftership.tracker import Tracking

    apikey = config[CONF_API_KEY]
    name = config[CONF_NAME]

    session = async_get_clientsession(hass)
    aftership = Tracking(hass.loop, session, apikey)

    await aftership.get_trackings()

    if not aftership.meta or aftership.meta['code'] != 200:
        _LOGGER.error("No tracking data found. Check API key is correct: %s",
                      aftership.meta)
        return

    async_add_entities([AfterShipSensor(aftership, name)], True)
Beispiel #4
0
async def example():
    """Get pending packages."""
    async with aiohttp.ClientSession() as session:
        pyaftership = Tracking(LOOP, session, API_KEY)
        packages = await pyaftership.get_trackings()
        print("Pending packages:", packages)