Member comments

This commit is contained in:
Aaron Bach 2018-11-07 20:35:46 -07:00
parent f44e31474a
commit 61a19d7966

View file

@ -98,6 +98,11 @@ class SeventeenTrackSummarySensor(Entity):
self._state = initial_state
self._status = status
@property
def available(self):
"""Return whether the entity is available."""
return bool(self._data.summary.get(self._status))
@property
def device_state_attributes(self):
"""Return the device state attributes."""
@ -133,7 +138,7 @@ class SeventeenTrackSummarySensor(Entity):
"""Update the sensor."""
await self._data.async_update()
self._state = self._data.summary[self._status]
self._state = self._data.summary.get(self._status)
class SeventeenTrackPackageSensor(Entity):
@ -154,6 +159,11 @@ class SeventeenTrackPackageSensor(Entity):
self._state = package.status
self._tracking_number = package.tracking_number
@property
def available(self):
"""Return whether the entity is available."""
return bool(self._data.packages.get(self._tracking_number))
@property
def device_state_attributes(self):
"""Return the device state attributes."""
@ -185,11 +195,9 @@ class SeventeenTrackPackageSensor(Entity):
await self._data.async_update()
try:
package = next(
iter([
package = next((
p for p in self._data.packages
if p.tracking_number == self._tracking_number
]))
if p.tracking_number == self._tracking_number))
except StopIteration:
# If the package no longer exists in the data, log a message and
# delete this entity:
@ -252,8 +260,8 @@ class SeventeenTrackData:
if not self.show_delivered:
packages = [p for p in packages if p.status != VALUE_DELIVERED]
# Add new packates:
to_add = list(set(packages) - set(self.packages))
# Add new packages:
to_add = set(packages) - set(self.packages)
if self.packages and to_add:
self._async_add_entities([
SeventeenTrackPackageSensor(self, package)
@ -263,7 +271,6 @@ class SeventeenTrackData:
self.packages = packages
except SeventeenTrackError as err:
_LOGGER.error('There was an error retrieving packages: %s', err)
self.packages = []
try:
self.summary = await self._client.profile.summary(
@ -271,4 +278,3 @@ class SeventeenTrackData:
_LOGGER.debug('New summary data received: %s', self.summary)
except SeventeenTrackError as err:
_LOGGER.error('There was an error retrieving the summary: %s', err)
self.summary = {}