From 1f863830e1c6ed29af34a24dbadce9725a3c4583 Mon Sep 17 00:00:00 2001 From: Malte Franken Date: Sat, 13 Oct 2018 01:48:02 +1100 Subject: [PATCH] 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 --- homeassistant/components/geo_location/__init__.py | 8 ++++++++ homeassistant/components/geo_location/demo.py | 7 +++++++ .../components/geo_location/geo_json_events.py | 6 ++++++ tests/components/geo_location/test_geo_json_events.py | 10 +++++++--- tests/components/geo_location/test_init.py | 4 ++++ 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/geo_location/__init__.py b/homeassistant/components/geo_location/__init__.py index 66753aad221..54aebc3591b 100644 --- a/homeassistant/components/geo_location/__init__.py +++ b/homeassistant/components/geo_location/__init__.py @@ -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 diff --git a/homeassistant/components/geo_location/demo.py b/homeassistant/components/geo_location/demo.py index ddec369e696..5e76e5cdf9a 100644 --- a/homeassistant/components/geo_location/demo.py +++ b/homeassistant/components/geo_location/demo.py @@ -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.""" diff --git a/homeassistant/components/geo_location/geo_json_events.py b/homeassistant/components/geo_location/geo_json_events.py index bb17fb2450e..fc40dddb9b8 100644 --- a/homeassistant/components/geo_location/geo_json_events.py +++ b/homeassistant/components/geo_location/geo_json_events.py @@ -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.""" diff --git a/tests/components/geo_location/test_geo_json_events.py b/tests/components/geo_location/test_geo_json_events.py index 5ce508289dd..dbaf71a6509 100644 --- a/tests/components/geo_location/test_geo_json_events.py +++ b/tests/components/geo_location/test_geo_json_events.py @@ -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, diff --git a/tests/components/geo_location/test_init.py b/tests/components/geo_location/test_init.py index 54efe977bf9..09030354901 100644 --- a/tests/components/geo_location/test_init.py +++ b/tests/components/geo_location/test_init.py @@ -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