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