mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use shorthand attributes in gpslogger device tracker (#126739)
This commit is contained in:
parent
ee75cba008
commit
f9f51e2381
@ -71,52 +71,25 @@ class GPSLoggerEntity(TrackerEntity, RestoreEntity):
|
|||||||
|
|
||||||
def __init__(self, device, location, battery, accuracy, attributes):
|
def __init__(self, device, location, battery, accuracy, attributes):
|
||||||
"""Set up GPSLogger entity."""
|
"""Set up GPSLogger entity."""
|
||||||
self._accuracy = accuracy
|
self._attr_location_accuracy = accuracy
|
||||||
self._attributes = attributes
|
self._attr_extra_state_attributes = attributes
|
||||||
self._name = device
|
self._name = device
|
||||||
self._battery = battery
|
self._battery = battery
|
||||||
self._location = location
|
if location:
|
||||||
|
self._attr_latitude = location[0]
|
||||||
|
self._attr_longitude = location[1]
|
||||||
self._unsub_dispatcher = None
|
self._unsub_dispatcher = None
|
||||||
self._unique_id = device
|
self._attr_unique_id = device
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
identifiers={(GPL_DOMAIN, device)},
|
||||||
|
name=device,
|
||||||
|
)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def battery_level(self):
|
def battery_level(self):
|
||||||
"""Return battery value of the device."""
|
"""Return battery value of the device."""
|
||||||
return self._battery
|
return self._battery
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self):
|
|
||||||
"""Return device specific attributes."""
|
|
||||||
return self._attributes
|
|
||||||
|
|
||||||
@property
|
|
||||||
def latitude(self):
|
|
||||||
"""Return latitude value of the device."""
|
|
||||||
return self._location[0]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def longitude(self):
|
|
||||||
"""Return longitude value of the device."""
|
|
||||||
return self._location[1]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def location_accuracy(self):
|
|
||||||
"""Return the gps accuracy of the device."""
|
|
||||||
return self._accuracy
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return the unique ID."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return the device info."""
|
|
||||||
return DeviceInfo(
|
|
||||||
identifiers={(GPL_DOMAIN, self._unique_id)},
|
|
||||||
name=self._name,
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register state update callback."""
|
"""Register state update callback."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
@ -125,13 +98,14 @@ class GPSLoggerEntity(TrackerEntity, RestoreEntity):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# don't restore if we got created with data
|
# don't restore if we got created with data
|
||||||
if self._location is not None:
|
if self.latitude is not None:
|
||||||
return
|
return
|
||||||
|
|
||||||
if (state := await self.async_get_last_state()) is None:
|
if (state := await self.async_get_last_state()) is None:
|
||||||
self._location = (None, None)
|
self._attr_latitude = None
|
||||||
self._accuracy = None
|
self._attr_longitude = None
|
||||||
self._attributes = {
|
self._attr_location_accuracy = 0
|
||||||
|
self._attr_extra_state_attributes = {
|
||||||
ATTR_ALTITUDE: None,
|
ATTR_ALTITUDE: None,
|
||||||
ATTR_ACTIVITY: None,
|
ATTR_ACTIVITY: None,
|
||||||
ATTR_DIRECTION: None,
|
ATTR_DIRECTION: None,
|
||||||
@ -142,9 +116,10 @@ class GPSLoggerEntity(TrackerEntity, RestoreEntity):
|
|||||||
return
|
return
|
||||||
|
|
||||||
attr = state.attributes
|
attr = state.attributes
|
||||||
self._location = (attr.get(ATTR_LATITUDE), attr.get(ATTR_LONGITUDE))
|
self._attr_latitude = attr.get(ATTR_LATITUDE)
|
||||||
self._accuracy = attr.get(ATTR_GPS_ACCURACY)
|
self._attr_longitude = attr.get(ATTR_LONGITUDE)
|
||||||
self._attributes = {
|
self._attr_location_accuracy = attr.get(ATTR_GPS_ACCURACY, 0)
|
||||||
|
self._attr_extra_state_attributes = {
|
||||||
ATTR_ALTITUDE: attr.get(ATTR_ALTITUDE),
|
ATTR_ALTITUDE: attr.get(ATTR_ALTITUDE),
|
||||||
ATTR_ACTIVITY: attr.get(ATTR_ACTIVITY),
|
ATTR_ACTIVITY: attr.get(ATTR_ACTIVITY),
|
||||||
ATTR_DIRECTION: attr.get(ATTR_DIRECTION),
|
ATTR_DIRECTION: attr.get(ATTR_DIRECTION),
|
||||||
@ -164,8 +139,9 @@ class GPSLoggerEntity(TrackerEntity, RestoreEntity):
|
|||||||
if device != self._name:
|
if device != self._name:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._location = location
|
self._attr_latitude = location[0]
|
||||||
|
self._attr_longitude = location[1]
|
||||||
self._battery = battery
|
self._battery = battery
|
||||||
self._accuracy = accuracy
|
self._attr_location_accuracy = accuracy
|
||||||
self._attributes.update(attributes)
|
self._attr_extra_state_attributes.update(attributes)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user