mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Add instance attributes to GeolocationEvent (#74389)
This commit is contained in:
parent
8d0e54d776
commit
18840c8af5
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import final
|
from typing import Any, final
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE
|
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):
|
class GeolocationEvent(Entity):
|
||||||
"""Base class for an external event with an associated geolocation."""
|
"""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
|
@property
|
||||||
def state(self):
|
def state(self) -> float | None:
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
if self.distance is not None:
|
if self.distance is not None:
|
||||||
return round(self.distance, 1)
|
return round(self.distance, 1)
|
||||||
@ -64,32 +71,30 @@ class GeolocationEvent(Entity):
|
|||||||
@property
|
@property
|
||||||
def source(self) -> str:
|
def source(self) -> str:
|
||||||
"""Return source value of this external event."""
|
"""Return source value of this external event."""
|
||||||
raise NotImplementedError
|
return self._attr_source
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def distance(self) -> float | None:
|
def distance(self) -> float | None:
|
||||||
"""Return distance value of this external event."""
|
"""Return distance value of this external event."""
|
||||||
return None
|
return self._attr_distance
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def latitude(self) -> float | None:
|
def latitude(self) -> float | None:
|
||||||
"""Return latitude value of this external event."""
|
"""Return latitude value of this external event."""
|
||||||
return None
|
return self._attr_latitude
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def longitude(self) -> float | None:
|
def longitude(self) -> float | None:
|
||||||
"""Return longitude value of this external event."""
|
"""Return longitude value of this external event."""
|
||||||
return None
|
return self._attr_longitude
|
||||||
|
|
||||||
@final
|
@final
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self) -> dict[str, Any]:
|
||||||
"""Return the state attributes of this external event."""
|
"""Return the state attributes of this external event."""
|
||||||
data = {}
|
data: dict[str, Any] = {ATTR_SOURCE: self.source}
|
||||||
if self.latitude is not None:
|
if self.latitude is not None:
|
||||||
data[ATTR_LATITUDE] = round(self.latitude, 5)
|
data[ATTR_LATITUDE] = round(self.latitude, 5)
|
||||||
if self.longitude is not None:
|
if self.longitude is not None:
|
||||||
data[ATTR_LONGITUDE] = round(self.longitude, 5)
|
data[ATTR_LONGITUDE] = round(self.longitude, 5)
|
||||||
if self.source is not None:
|
|
||||||
data[ATTR_SOURCE] = self.source
|
|
||||||
return data
|
return data
|
||||||
|
@ -20,5 +20,5 @@ async def test_event(hass):
|
|||||||
assert entity.distance is None
|
assert entity.distance is None
|
||||||
assert entity.latitude is None
|
assert entity.latitude is None
|
||||||
assert entity.longitude is None
|
assert entity.longitude is None
|
||||||
with pytest.raises(NotImplementedError):
|
with pytest.raises(AttributeError):
|
||||||
assert entity.source is None
|
assert entity.source is None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user