Skip to content

Commit

Permalink
Minor updates (#17437)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabaff authored and Danielhiversen committed Oct 14, 2018
1 parent 5ac0469 commit daf48a3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 64 deletions.
13 changes: 7 additions & 6 deletions homeassistant/components/geo_location/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/geo_location/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
37 changes: 17 additions & 20 deletions homeassistant/components/geo_location/geo_json_events.py
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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),
})


Expand All @@ -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 = []
Expand All @@ -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."""
Expand All @@ -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([])

Expand Down
Original file line number Diff line number Diff line change
@@ -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']
Expand All @@ -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'
Expand All @@ -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)

Expand All @@ -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),
})


Expand All @@ -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."""
Expand All @@ -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 = {}
Expand All @@ -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)
Expand All @@ -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())

Expand All @@ -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):
Expand Down

0 comments on commit daf48a3

Please sign in to comment.