From daf48a3b1fdfe382c2435b91663278ad60d06079 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Sun, 14 Oct 2018 17:10:46 +0200 Subject: [PATCH] Minor updates (#17437) --- .../components/geo_location/__init__.py | 13 ++-- homeassistant/components/geo_location/demo.py | 6 +- .../geo_location/geo_json_events.py | 37 +++++----- .../nsw_rural_fire_service_feed.py | 70 +++++++++---------- 4 files changed, 62 insertions(+), 64 deletions(-) diff --git a/homeassistant/components/geo_location/__init__.py b/homeassistant/components/geo_location/__init__.py index 54aebc3591b943..495c9e1744b42c 100644 --- a/homeassistant/components/geo_location/__init__.py +++ b/homeassistant/components/geo_location/__init__.py @@ -1,33 +1,34 @@ """ Geo Location component. -This component covers platforms that deal with external events that contain -a geo location related to the installed HA instance. - For more details about this component, please refer to the documentation at https://home-assistant.io/components/geo_location/ """ -import logging from datetime import timedelta +import logging from typing import Optional from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE +from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_component import EntityComponent -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' + SCAN_INTERVAL = timedelta(seconds=60) async def async_setup(hass, config): - """Set up this component.""" + """Set up the Geo Location component.""" component = EntityComponent( _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_EVENTS) await component.async_setup(config) diff --git a/homeassistant/components/geo_location/demo.py b/homeassistant/components/geo_location/demo.py index 5e76e5cdf9ac37..6c5cc2fe147d95 100644 --- a/homeassistant/components/geo_location/demo.py +++ b/homeassistant/components/geo_location/demo.py @@ -4,10 +4,10 @@ For more details about this platform, please refer to the documentation https://home-assistant.io/components/demo/ """ +from datetime import timedelta import logging +from math import cos, pi, radians, sin import random -from datetime import timedelta -from math import pi, cos, sin, radians from typing import Optional from homeassistant.components.geo_location import GeoLocationEvent @@ -16,7 +16,7 @@ _LOGGER = logging.getLogger(__name__) AVG_KM_PER_DEGREE = 111.0 -DEFAULT_UNIT_OF_MEASUREMENT = "km" +DEFAULT_UNIT_OF_MEASUREMENT = 'km' DEFAULT_UPDATE_INTERVAL = timedelta(minutes=1) MAX_RADIUS_IN_KM = 50 NUMBER_OF_DEMO_DEVICES = 5 diff --git a/homeassistant/components/geo_location/geo_json_events.py b/homeassistant/components/geo_location/geo_json_events.py index fc40dddb9b88c2..4665f8f236107c 100644 --- a/homeassistant/components/geo_location/geo_json_events.py +++ b/homeassistant/components/geo_location/geo_json_events.py @@ -1,24 +1,20 @@ """ Generic GeoJSON events platform. -Retrieves current events (typically incidents or alerts) in GeoJSON format, and -displays information on events filtered by distance to the HA instance's -location. - For more details about this platform, please refer to the documentation at https://home-assistant.io/components/geo_location/geo_json_events/ """ -import logging from datetime import timedelta +import logging from typing import Optional import voluptuous as vol +from homeassistant.components.geo_location import ( + PLATFORM_SCHEMA, GeoLocationEvent) +from homeassistant.const import ( + CONF_RADIUS, CONF_SCAN_INTERVAL, CONF_URL, EVENT_HOMEASSISTANT_START) import homeassistant.helpers.config_validation as cv -from homeassistant.components.geo_location import GeoLocationEvent -from homeassistant.const import CONF_RADIUS, CONF_URL, CONF_SCAN_INTERVAL, \ - EVENT_HOMEASSISTANT_START -from homeassistant.components.geo_location import PLATFORM_SCHEMA from homeassistant.helpers.event import track_time_interval REQUIREMENTS = ['geojson_client==0.1'] @@ -28,15 +24,15 @@ ATTR_EXTERNAL_ID = 'external_id' DEFAULT_RADIUS_IN_KM = 20.0 -DEFAULT_UNIT_OF_MEASUREMENT = "km" +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, - vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS_IN_KM): - vol.Coerce(float), + vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS_IN_KM): vol.Coerce(float), }) @@ -56,8 +52,9 @@ def __init__(self, hass, add_entities, scan_interval, url, radius_in_km): """Initialize the GeoJSON Feed Manager.""" from geojson_client.generic_feed import GenericFeed self._hass = hass - self._feed = GenericFeed((hass.config.latitude, hass.config.longitude), - filter_radius=radius_in_km, url=url) + self._feed = GenericFeed( + (hass.config.latitude, hass.config.longitude), + filter_radius=radius_in_km, url=url) self._add_entities = add_entities self._scan_interval = scan_interval self._feed_entries = [] @@ -68,8 +65,8 @@ def __init__(self, hass, add_entities, scan_interval, url, radius_in_km): def _init_regular_updates(self): """Schedule regular updates at the specified interval.""" - track_time_interval(self._hass, lambda now: self._update(), - self._scan_interval) + track_time_interval( + self._hass, lambda now: self._update(), self._scan_interval) def _update(self): """Update the feed and then update connected entities.""" @@ -82,11 +79,11 @@ def _update(self): keep_entries = self._update_or_remove_entities(feed_entries) self._generate_new_entities(keep_entries) elif status == geojson_client.UPDATE_OK_NO_DATA: - _LOGGER.debug("Update successful, but no data received from %s", - self._feed) + _LOGGER.debug( + "Update successful, but no data received from %s", self._feed) else: - _LOGGER.warning("Update not successful, no data received from %s", - self._feed) + _LOGGER.warning( + "Update not successful, no data received from %s", self._feed) # Remove all entities. self._update_or_remove_entities([]) diff --git a/homeassistant/components/geo_location/nsw_rural_fire_service_feed.py b/homeassistant/components/geo_location/nsw_rural_fire_service_feed.py index 52f261ba858de8..d3b13abe70470b 100644 --- a/homeassistant/components/geo_location/nsw_rural_fire_service_feed.py +++ b/homeassistant/components/geo_location/nsw_rural_fire_service_feed.py @@ -1,27 +1,24 @@ """ NSW Rural Fire Service Feed platform. -Retrieves current events (bush fires, grass fires, etc.) in GeoJSON format, -and displays information on events filtered by distance and category to the -HA instance's location. - For more details about this platform, please refer to the documentation at https://home-assistant.io/components/geo_location/nsw_rural_fire_service_feed/ """ -import logging from datetime import timedelta +import logging from typing import Optional import voluptuous as vol -import homeassistant.helpers.config_validation as cv -from homeassistant.components.geo_location import GeoLocationEvent, \ - PLATFORM_SCHEMA -from homeassistant.const import CONF_RADIUS, CONF_SCAN_INTERVAL, \ - EVENT_HOMEASSISTANT_START, ATTR_ATTRIBUTION +from homeassistant.components.geo_location import ( + PLATFORM_SCHEMA, GeoLocationEvent) +from homeassistant.const import ( + ATTR_ATTRIBUTION, ATTR_LOCATION, CONF_RADIUS, CONF_SCAN_INTERVAL, + EVENT_HOMEASSISTANT_START) from homeassistant.core import callback -from homeassistant.helpers.dispatcher import dispatcher_send, \ - async_dispatcher_connect +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.dispatcher import ( + async_dispatcher_connect, dispatcher_send) from homeassistant.helpers.event import track_time_interval REQUIREMENTS = ['geojson_client==0.1'] @@ -32,7 +29,6 @@ ATTR_COUNCIL_AREA = 'council_area' ATTR_EXTERNAL_ID = 'external_id' ATTR_FIRE = 'fire' -ATTR_LOCATION = 'location' ATTR_PUBLICATION_DATE = 'publication_date' ATTR_RESPONSIBLE_AGENCY = 'responsible_agency' ATTR_SIZE = 'size' @@ -42,7 +38,7 @@ CONF_CATEGORIES = 'categories' DEFAULT_RADIUS_IN_KM = 20.0 -DEFAULT_UNIT_OF_MEASUREMENT = "km" +DEFAULT_UNIT_OF_MEASUREMENT = 'km' SCAN_INTERVAL = timedelta(minutes=5) @@ -51,14 +47,17 @@ SOURCE = 'nsw_rural_fire_service_feed' -VALID_CATEGORIES = ['Emergency Warning', 'Watch and Act', 'Advice', - 'Not Applicable'] +VALID_CATEGORIES = [ + 'Advice', + 'Emergency Warning', + 'Not Applicable', + 'Watch and Act', +] PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ - vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS_IN_KM): - vol.Coerce(float), vol.Optional(CONF_CATEGORIES, default=[]): - vol.All(cv.ensure_list, [vol.In(VALID_CATEGORIES)]) + vol.All(cv.ensure_list, [vol.In(VALID_CATEGORIES)]), + vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS_IN_KM): vol.Coerce(float), }) @@ -68,8 +67,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): radius_in_km = config[CONF_RADIUS] categories = config.get(CONF_CATEGORIES) # Initialize the entity manager. - feed = NswRuralFireServiceFeedManager(hass, add_entities, scan_interval, - radius_in_km, categories) + feed = NswRuralFireServiceFeedManager( + hass, add_entities, scan_interval, radius_in_km, categories) def start_feed_manager(event): """Start feed manager.""" @@ -86,11 +85,11 @@ def __init__(self, hass, add_entities, scan_interval, radius_in_km, """Initialize the GeoJSON Feed Manager.""" from geojson_client.nsw_rural_fire_service_feed \ import NswRuralFireServiceFeed + self._hass = hass - self._feed = NswRuralFireServiceFeed((hass.config.latitude, - hass.config.longitude), - filter_radius=radius_in_km, - filter_categories=categories) + self._feed = NswRuralFireServiceFeed( + (hass.config.latitude, hass.config.longitude), + filter_radius=radius_in_km, filter_categories=categories) self._add_entities = add_entities self._scan_interval = scan_interval self.feed_entries = {} @@ -103,12 +102,13 @@ def startup(self): def _init_regular_updates(self): """Schedule regular updates at the specified interval.""" - track_time_interval(self._hass, lambda now: self._update(), - self._scan_interval) + track_time_interval( + self._hass, lambda now: self._update(), self._scan_interval) def _update(self): """Update the feed and then update connected entities.""" import geojson_client + status, feed_entries = self._feed.update() if status == geojson_client.UPDATE_OK: _LOGGER.debug("Data retrieved %s", feed_entries) @@ -127,11 +127,11 @@ def _update(self): self._managed_external_ids) self._generate_new_entities(create_external_ids) elif status == geojson_client.UPDATE_OK_NO_DATA: - _LOGGER.debug("Update successful, but no data received from %s", - self._feed) + _LOGGER.debug( + "Update successful, but no data received from %s", self._feed) else: - _LOGGER.warning("Update not successful, no data received from %s", - self._feed) + _LOGGER.warning( + "Update not successful, no data received from %s", self._feed) # Remove all entities. self._remove_entities(self._managed_external_ids.copy()) @@ -150,16 +150,16 @@ def _update_entities(self, external_ids): """Update entities.""" for external_id in external_ids: _LOGGER.debug("Existing entity found %s", external_id) - dispatcher_send(self._hass, - SIGNAL_UPDATE_ENTITY.format(external_id)) + dispatcher_send( + self._hass, SIGNAL_UPDATE_ENTITY.format(external_id)) def _remove_entities(self, external_ids): """Remove entities.""" for external_id in external_ids: _LOGGER.debug("Entity not current anymore %s", external_id) self._managed_external_ids.remove(external_id) - dispatcher_send(self._hass, - SIGNAL_DELETE_ENTITY.format(external_id)) + dispatcher_send( + self._hass, SIGNAL_DELETE_ENTITY.format(external_id)) class NswRuralFireServiceLocationEvent(GeoLocationEvent):