Add instance attributes to GeolocationEvent (#74389)

This commit is contained in:
epenet 2022-07-04 16:43:53 +02:00 committed by GitHub
parent 8d0e54d776
commit 18840c8af5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 11 deletions

View File

@ -3,7 +3,7 @@ from __future__ import annotations
from datetime import timedelta
import logging
from typing import final
from typing import Any, final
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
@ -54,8 +54,15 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class GeolocationEvent(Entity):
"""Base class for an external event with an associated geolocation."""
# Entity Properties
_attr_source: str
_attr_distance: float | None = None
_attr_latitude: float | None = None
_attr_longitude: float | None = None
@final
@property
def state(self):
def state(self) -> float | None:
"""Return the state of the sensor."""
if self.distance is not None:
return round(self.distance, 1)
@ -64,32 +71,30 @@ class GeolocationEvent(Entity):
@property
def source(self) -> str:
"""Return source value of this external event."""
raise NotImplementedError
return self._attr_source
@property
def distance(self) -> float | None:
"""Return distance value of this external event."""
return None
return self._attr_distance
@property
def latitude(self) -> float | None:
"""Return latitude value of this external event."""
return None
return self._attr_latitude
@property
def longitude(self) -> float | None:
"""Return longitude value of this external event."""
return None
return self._attr_longitude
@final
@property
def state_attributes(self):
def state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of this external event."""
data = {}
data: dict[str, Any] = {ATTR_SOURCE: self.source}
if self.latitude is not None:
data[ATTR_LATITUDE] = round(self.latitude, 5)
if self.longitude is not None:
data[ATTR_LONGITUDE] = round(self.longitude, 5)
if self.source is not None:
data[ATTR_SOURCE] = self.source
return data

View File

@ -20,5 +20,5 @@ async def test_event(hass):
assert entity.distance is None
assert entity.latitude is None
assert entity.longitude is None
with pytest.raises(NotImplementedError):
with pytest.raises(AttributeError):
assert entity.source is None