Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions homeassistant/components/seventeentrack/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
REQUIREMENTS = ['py17track==2.2.2']
_LOGGER = logging.getLogger(__name__)

DOMAIN = 'seventeentrack'
Comment thread
bachya marked this conversation as resolved.
Outdated

ATTR_DESTINATION_COUNTRY = 'destination_country'
ATTR_FRIENDLY_NAME = 'friendly_name'
ATTR_INFO_TEXT = 'info_text'
Expand All @@ -34,6 +36,8 @@
DEFAULT_ATTRIBUTION = 'Data provided by 17track.net'
DEFAULT_SCAN_INTERVAL = timedelta(minutes=10)

ENTITY_ID_TEMPLATE = 'package_{0}_{1}'

NOTIFICATION_DELIVERED_ID_SCAFFOLD = 'package_delivered_{0}'
NOTIFICATION_DELIVERED_TITLE = 'Package Delivered'
NOTIFICATION_DELIVERED_URL_SCAFFOLD = 'https://t.17track.net/track#nums={0}'
Expand Down Expand Up @@ -72,8 +76,8 @@ async def async_setup_platform(
scan_interval = config.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)

data = SeventeenTrackData(
client, async_add_entities, scan_interval, config[CONF_SHOW_ARCHIVED],
config[CONF_SHOW_DELIVERED])
client, hass, async_add_entities, scan_interval,
config[CONF_SHOW_ARCHIVED], config[CONF_SHOW_DELIVERED])
await data.async_update()

sensors = []
Expand Down Expand Up @@ -209,7 +213,7 @@ def state(self):
@property
def unique_id(self):
"""Return a unique, HASS-friendly identifier for this entity."""
return 'package_{0}_{1}'.format(
return ENTITY_ID_TEMPLATE.format(
self._data.account_id, self._tracking_number)

async def async_update(self):
Expand Down Expand Up @@ -263,11 +267,12 @@ class SeventeenTrackData:
"""Define a data handler for 17track.net."""

def __init__(
self, client, async_add_entities, scan_interval, show_archived,
show_delivered):
self, client, hass, async_add_entities, scan_interval,
Comment thread
bachya marked this conversation as resolved.
Outdated
show_archived, show_delivered):
"""Initialize."""
self._async_add_entities = async_add_entities
self._client = client
self._hass = hass
self._scan_interval = scan_interval
self._show_archived = show_archived
self.account_id = client.profile.account_id
Expand Down Expand Up @@ -297,6 +302,17 @@ async def _async_update(self):
for package in to_add
], True)

# Remove archived packages from the entity registry:
to_remove = set(self.packages) - set(packages)
reg = self._hass.helpers.entity_registry.async_get_registry()
for package in to_remove:
entity_id = reg.async_get_entity_id(
self, DOMAIN, 'sensor', ENTITY_ID_TEMPLATE.format(
self.account_id, package.tracking_number))
if not entity_id:
continue
self._hass.async_create_task(reg.async_remove(entity_id))
Comment thread
MartinHjelmare marked this conversation as resolved.

self.packages = packages
except SeventeenTrackError as err:
_LOGGER.error('There was an error retrieving packages: %s', err)
Expand Down