diff --git a/homeassistant/components/geo_location/__init__.py b/homeassistant/components/geo_location/__init__.py index e58391ca84a..54542fa8503 100644 --- a/homeassistant/components/geo_location/__init__.py +++ b/homeassistant/components/geo_location/__init__.py @@ -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 diff --git a/tests/components/geo_location/test_init.py b/tests/components/geo_location/test_init.py index 00cb2a872d2..f2cb5c6b108 100644 --- a/tests/components/geo_location/test_init.py +++ b/tests/components/geo_location/test_init.py @@ -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