mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use shorthand attributes in geofency device tracker (#126741)
This commit is contained in:
parent
77db88ad28
commit
a1e6d4b693
@ -57,44 +57,17 @@ class GeofencyEntity(TrackerEntity, RestoreEntity):
|
|||||||
|
|
||||||
def __init__(self, device, gps=None, location_name=None, attributes=None):
|
def __init__(self, device, gps=None, location_name=None, attributes=None):
|
||||||
"""Set up Geofency entity."""
|
"""Set up Geofency entity."""
|
||||||
self._attributes = attributes or {}
|
self._attr_extra_state_attributes = attributes or {}
|
||||||
self._name = device
|
self._name = device
|
||||||
self._location_name = location_name
|
self._attr_location_name = location_name
|
||||||
self._gps = gps
|
if gps:
|
||||||
|
self._attr_latitude = gps[0]
|
||||||
|
self._attr_longitude = gps[1]
|
||||||
self._unsub_dispatcher = None
|
self._unsub_dispatcher = None
|
||||||
self._unique_id = device
|
self._attr_unique_id = device
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
@property
|
identifiers={(GF_DOMAIN, device)},
|
||||||
def extra_state_attributes(self):
|
name=device,
|
||||||
"""Return device specific attributes."""
|
|
||||||
return self._attributes
|
|
||||||
|
|
||||||
@property
|
|
||||||
def latitude(self):
|
|
||||||
"""Return latitude value of the device."""
|
|
||||||
return self._gps[0]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def longitude(self):
|
|
||||||
"""Return longitude value of the device."""
|
|
||||||
return self._gps[1]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def location_name(self):
|
|
||||||
"""Return a location name for the current location of the device."""
|
|
||||||
return self._location_name
|
|
||||||
|
|
||||||
@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={(GF_DOMAIN, self._unique_id)},
|
|
||||||
name=self._name,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
@ -104,21 +77,23 @@ class GeofencyEntity(TrackerEntity, RestoreEntity):
|
|||||||
self.hass, TRACKER_UPDATE, self._async_receive_data
|
self.hass, TRACKER_UPDATE, self._async_receive_data
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._attributes:
|
if self._attr_extra_state_attributes:
|
||||||
return
|
return
|
||||||
|
|
||||||
if (state := await self.async_get_last_state()) is None:
|
if (state := await self.async_get_last_state()) is None:
|
||||||
self._gps = (None, None)
|
self._attr_latitude = None
|
||||||
|
self._attr_longitude = None
|
||||||
return
|
return
|
||||||
|
|
||||||
attr = state.attributes
|
attr = state.attributes
|
||||||
self._gps = (attr.get(ATTR_LATITUDE), attr.get(ATTR_LONGITUDE))
|
self._attr_latitude = attr.get(ATTR_LATITUDE)
|
||||||
|
self._attr_longitude = attr.get(ATTR_LONGITUDE)
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self) -> None:
|
async def async_will_remove_from_hass(self) -> None:
|
||||||
"""Clean up after entity before removal."""
|
"""Clean up after entity before removal."""
|
||||||
await super().async_will_remove_from_hass()
|
await super().async_will_remove_from_hass()
|
||||||
self._unsub_dispatcher()
|
self._unsub_dispatcher()
|
||||||
self.hass.data[GF_DOMAIN]["devices"].remove(self._unique_id)
|
self.hass.data[GF_DOMAIN]["devices"].remove(self.unique_id)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_receive_data(self, device, gps, location_name, attributes):
|
def _async_receive_data(self, device, gps, location_name, attributes):
|
||||||
@ -126,7 +101,8 @@ class GeofencyEntity(TrackerEntity, RestoreEntity):
|
|||||||
if device != self._name:
|
if device != self._name:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._attributes.update(attributes)
|
self._attr_extra_state_attributes.update(attributes)
|
||||||
self._location_name = location_name
|
self._attr_location_name = location_name
|
||||||
self._gps = gps
|
self._attr_latitude = gps[0]
|
||||||
|
self._attr_longitude = gps[1]
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user