mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Adding source attribute to geo location platforms (#17339)
* added source attribute to all geo_location platforms * amended test cases * constant moved and source method now forces subclasses to override
This commit is contained in:
parent
6971e84ddf
commit
1f863830e1
@ -19,6 +19,7 @@ from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_DISTANCE = 'distance'
|
||||
ATTR_SOURCE = 'source'
|
||||
DOMAIN = 'geo_location'
|
||||
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||
GROUP_NAME_ALL_EVENTS = 'All Geo Location Events'
|
||||
@ -43,6 +44,11 @@ class GeoLocationEvent(Entity):
|
||||
return round(self.distance, 1)
|
||||
return None
|
||||
|
||||
@property
|
||||
def source(self) -> str:
|
||||
"""Return source value of this external event."""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def distance(self) -> Optional[float]:
|
||||
"""Return distance value of this external event."""
|
||||
@ -66,4 +72,6 @@ class GeoLocationEvent(Entity):
|
||||
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
|
||||
|
@ -26,6 +26,8 @@ EVENT_NAMES = ["Bushfire", "Hazard Reduction", "Grass Fire", "Burn off",
|
||||
"Cyclone", "Waterspout", "Dust Storm", "Blizzard", "Ice Storm",
|
||||
"Earthquake", "Tsunami"]
|
||||
|
||||
SOURCE = 'demo'
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
"""Set up the Demo geo locations."""
|
||||
@ -100,6 +102,11 @@ class DemoGeoLocationEvent(GeoLocationEvent):
|
||||
self._longitude = longitude
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
|
||||
@property
|
||||
def source(self) -> str:
|
||||
"""Return source value of this external event."""
|
||||
return SOURCE
|
||||
|
||||
@property
|
||||
def name(self) -> Optional[str]:
|
||||
"""Return the name of the event."""
|
||||
|
@ -31,6 +31,7 @@ DEFAULT_RADIUS_IN_KM = 20.0
|
||||
DEFAULT_UNIT_OF_MEASUREMENT = "km"
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=5)
|
||||
SOURCE = 'geo_json_events'
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_URL): cv.string,
|
||||
@ -162,6 +163,11 @@ class GeoJsonLocationEvent(GeoLocationEvent):
|
||||
self._longitude = feed_entry.coordinates[1]
|
||||
self.external_id = feed_entry.external_id
|
||||
|
||||
@property
|
||||
def source(self) -> str:
|
||||
"""Return source value of this external event."""
|
||||
return SOURCE
|
||||
|
||||
@property
|
||||
def name(self) -> Optional[str]:
|
||||
"""Return the name of the entity."""
|
||||
|
@ -4,6 +4,7 @@ from unittest import mock
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from homeassistant.components import geo_location
|
||||
from homeassistant.components.geo_location import ATTR_SOURCE
|
||||
from homeassistant.components.geo_location.geo_json_events import \
|
||||
SCAN_INTERVAL, ATTR_EXTERNAL_ID
|
||||
from homeassistant.const import CONF_URL, EVENT_HOMEASSISTANT_START, \
|
||||
@ -84,7 +85,8 @@ class TestGeoJsonPlatform(unittest.TestCase):
|
||||
assert state.attributes == {
|
||||
ATTR_EXTERNAL_ID: "1234", ATTR_LATITUDE: -31.0,
|
||||
ATTR_LONGITUDE: 150.0, ATTR_FRIENDLY_NAME: "Title 1",
|
||||
ATTR_UNIT_OF_MEASUREMENT: "km"}
|
||||
ATTR_UNIT_OF_MEASUREMENT: "km",
|
||||
ATTR_SOURCE: 'geo_json_events'}
|
||||
self.assertAlmostEqual(float(state.state), 15.5)
|
||||
|
||||
state = self.hass.states.get("geo_location.title_2")
|
||||
@ -93,7 +95,8 @@ class TestGeoJsonPlatform(unittest.TestCase):
|
||||
assert state.attributes == {
|
||||
ATTR_EXTERNAL_ID: "2345", ATTR_LATITUDE: -31.1,
|
||||
ATTR_LONGITUDE: 150.1, ATTR_FRIENDLY_NAME: "Title 2",
|
||||
ATTR_UNIT_OF_MEASUREMENT: "km"}
|
||||
ATTR_UNIT_OF_MEASUREMENT: "km",
|
||||
ATTR_SOURCE: 'geo_json_events'}
|
||||
self.assertAlmostEqual(float(state.state), 20.5)
|
||||
|
||||
state = self.hass.states.get("geo_location.title_3")
|
||||
@ -102,7 +105,8 @@ class TestGeoJsonPlatform(unittest.TestCase):
|
||||
assert state.attributes == {
|
||||
ATTR_EXTERNAL_ID: "3456", ATTR_LATITUDE: -31.2,
|
||||
ATTR_LONGITUDE: 150.2, ATTR_FRIENDLY_NAME: "Title 3",
|
||||
ATTR_UNIT_OF_MEASUREMENT: "km"}
|
||||
ATTR_UNIT_OF_MEASUREMENT: "km",
|
||||
ATTR_SOURCE: 'geo_json_events'}
|
||||
self.assertAlmostEqual(float(state.state), 25.5)
|
||||
|
||||
# Simulate an update - one existing, one new entry,
|
||||
|
@ -1,4 +1,6 @@
|
||||
"""The tests for the geo location component."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import geo_location
|
||||
from homeassistant.components.geo_location import GeoLocationEvent
|
||||
from homeassistant.setup import async_setup_component
|
||||
@ -18,3 +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):
|
||||
assert entity.source is None
|
||||
|
Loading…
x
Reference in New Issue
Block a user