GeoRSS events sensor refactored (#16939)
* refactored geo_rss_events sensor to make use of new georss-client library that handles the communication with the rss feed * fixed lint error
This commit is contained in:
1 parent
b0b3620b2b
commit
13af61e103
6 files changed
+154
-316
No files matched your search
@@ -9,7 +9,6 @@ For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.geo_rss_events/
|
||||
"""
|
||||
import logging
|
||||
from collections import namedtuple
|
||||
from datetime import timedelta
|
||||
|
||||
import voluptuous as vol
|
||||
@@ -19,9 +18,8 @@ from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
STATE_UNKNOWN, CONF_UNIT_OF_MEASUREMENT, CONF_NAME, CONF_RADIUS, CONF_URL)
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
REQUIREMENTS = ['feedparser==5.2.1', 'haversine==0.4.5']
|
||||
REQUIREMENTS = ['georss_client==0.1']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -38,9 +36,6 @@ DEFAULT_UNIT_OF_MEASUREMENT = 'Events'
|
||||
|
||||
DOMAIN = 'geo_rss_events'
|
||||
|
||||
# Minimum time between updates from the source.
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=5)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
@@ -67,18 +62,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
_LOGGER.debug("latitude=%s, longitude=%s, url=%s, radius=%s",
|
||||
home_latitude, home_longitude, url, radius_in_km)
|
||||
|
||||
# Initialise update service.
|
||||
data = GeoRssServiceData(home_latitude, home_longitude, url, radius_in_km)
|
||||
data.update()
|
||||
|
||||
# Create all sensors based on categories.
|
||||
devices = []
|
||||
if not categories:
|
||||
device = GeoRssServiceSensor(None, data, name, unit_of_measurement)
|
||||
device = GeoRssServiceSensor((home_latitude, home_longitude), url,
|
||||
radius_in_km, None, name,
|
||||
unit_of_measurement)
|
||||
devices.append(device)
|
||||
else:
|
||||
for category in categories:
|
||||
device = GeoRssServiceSensor(category, data, name,
|
||||
device = GeoRssServiceSensor((home_latitude, home_longitude), url,
|
||||
radius_in_km, category, name,
|
||||
unit_of_measurement)
|
||||
devices.append(device)
|
||||
add_entities(devices, True)
|
||||
@@ -87,14 +81,18 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
class GeoRssServiceSensor(Entity):
|
||||
"""Representation of a Sensor."""
|
||||
|
||||
def __init__(self, category, data, service_name, unit_of_measurement):
|
||||
def __init__(self, home_coordinates, url, radius, category, service_name,
|
||||
unit_of_measurement):
|
||||
"""Initialize the sensor."""
|
||||
self._category = category
|
||||
self._data = data
|
||||
self._service_name = service_name
|
||||
self._state = STATE_UNKNOWN
|
||||
self._state_attributes = None
|
||||
self._unit_of_measurement = unit_of_measurement
|
||||
from georss_client.generic_feed import GenericFeed
|
||||
self._feed = GenericFeed(home_coordinates, url, filter_radius=radius,
|
||||
filter_categories=None if not category
|
||||
else [category])
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@@ -125,115 +123,25 @@ class GeoRssServiceSensor(Entity):
|
||||
|
||||
def update(self):
|
||||
"""Update this sensor from the GeoRSS service."""
|
||||
_LOGGER.debug("About to update sensor %s", self.entity_id)
|
||||
self._data.update()
|
||||
# If no events were found due to an error then just set state to zero.
|
||||
if self._data.events is None:
|
||||
self._state = 0
|
||||
else:
|
||||
if self._category is None:
|
||||
# Add all events regardless of category.
|
||||
my_events = self._data.events
|
||||
else:
|
||||
# Only keep events that belong to sensor's category.
|
||||
my_events = [event for event in self._data.events if
|
||||
event[ATTR_CATEGORY] == self._category]
|
||||
import georss_client
|
||||
status, feed_entries = self._feed.update()
|
||||
if status == georss_client.UPDATE_OK:
|
||||
_LOGGER.debug("Adding events to sensor %s: %s", self.entity_id,
|
||||
my_events)
|
||||
self._state = len(my_events)
|
||||
feed_entries)
|
||||
self._state = len(feed_entries)
|
||||
# And now compute the attributes from the filtered events.
|
||||
matrix = {}
|
||||
for event in my_events:
|
||||
matrix[event[ATTR_TITLE]] = '{:.0f}km'.format(
|
||||
event[ATTR_DISTANCE])
|
||||
for entry in feed_entries:
|
||||
matrix[entry.title] = '{:.0f}km'.format(
|
||||
entry.distance_to_home)
|
||||
self._state_attributes = matrix
|
||||
|
||||
|
||||
class GeoRssServiceData:
|
||||
"""Provide access to GeoRSS feed and stores the latest data."""
|
||||
|
||||
def __init__(self, home_latitude, home_longitude, url, radius_in_km):
|
||||
"""Initialize the update service."""
|
||||
self._home_coordinates = [home_latitude, home_longitude]
|
||||
self._url = url
|
||||
self._radius_in_km = radius_in_km
|
||||
self.events = None
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
def update(self):
|
||||
"""Retrieve data from GeoRSS feed and store events."""
|
||||
import feedparser
|
||||
feed_data = feedparser.parse(self._url)
|
||||
if not feed_data:
|
||||
_LOGGER.error("Error fetching feed data from %s", self._url)
|
||||
elif status == georss_client.UPDATE_OK_NO_DATA:
|
||||
_LOGGER.debug("Update successful, but no data received from %s",
|
||||
self._feed)
|
||||
# Don't change the state or state attributes.
|
||||
else:
|
||||
events = self.filter_entries(feed_data)
|
||||
self.events = events
|
||||
|
||||
def filter_entries(self, feed_data):
|
||||
"""Filter entries by distance from home coordinates."""
|
||||
events = []
|
||||
_LOGGER.debug("%s entri(es) available in feed %s",
|
||||
len(feed_data.entries), self._url)
|
||||
for entry in feed_data.entries:
|
||||
geometry = None
|
||||
if hasattr(entry, 'where'):
|
||||
geometry = entry.where
|
||||
elif hasattr(entry, 'geo_lat') and hasattr(entry, 'geo_long'):
|
||||
coordinates = (float(entry.geo_long), float(entry.geo_lat))
|
||||
point = namedtuple('Point', ['type', 'coordinates'])
|
||||
geometry = point('Point', coordinates)
|
||||
if geometry:
|
||||
distance = self.calculate_distance_to_geometry(geometry)
|
||||
if distance <= self._radius_in_km:
|
||||
event = {
|
||||
ATTR_CATEGORY: None if not hasattr(
|
||||
entry, 'category') else entry.category,
|
||||
ATTR_TITLE: None if not hasattr(
|
||||
entry, 'title') else entry.title,
|
||||
ATTR_DISTANCE: distance
|
||||
}
|
||||
events.append(event)
|
||||
_LOGGER.debug("%s events found nearby", len(events))
|
||||
return events
|
||||
|
||||
def calculate_distance_to_geometry(self, geometry):
|
||||
"""Calculate the distance between HA and provided geometry."""
|
||||
distance = float("inf")
|
||||
if geometry.type == 'Point':
|
||||
distance = self.calculate_distance_to_point(geometry)
|
||||
elif geometry.type == 'Polygon':
|
||||
distance = self.calculate_distance_to_polygon(
|
||||
geometry.coordinates[0])
|
||||
else:
|
||||
_LOGGER.warning("Not yet implemented: %s", geometry.type)
|
||||
return distance
|
||||
|
||||
def calculate_distance_to_point(self, point):
|
||||
"""Calculate the distance between HA and the provided point."""
|
||||
# Swap coordinates to match: (lat, lon).
|
||||
coordinates = (point.coordinates[1], point.coordinates[0])
|
||||
return self.calculate_distance_to_coords(coordinates)
|
||||
|
||||
def calculate_distance_to_coords(self, coordinates):
|
||||
"""Calculate the distance between HA and the provided coordinates."""
|
||||
# Expecting coordinates in format: (lat, lon).
|
||||
from haversine import haversine
|
||||
distance = haversine(coordinates, self._home_coordinates)
|
||||
_LOGGER.debug("Distance from %s to %s: %s km", self._home_coordinates,
|
||||
coordinates, distance)
|
||||
return distance
|
||||
|
||||
def calculate_distance_to_polygon(self, polygon):
|
||||
"""Calculate the distance between HA and the provided polygon."""
|
||||
distance = float("inf")
|
||||
# Calculate distance from polygon by calculating the distance
|
||||
# to each point of the polygon but not to each edge of the
|
||||
# polygon; should be good enough
|
||||
for polygon_point in polygon:
|
||||
coordinates = (polygon_point[1], polygon_point[0])
|
||||
distance = min(distance,
|
||||
self.calculate_distance_to_coords(coordinates))
|
||||
_LOGGER.debug("Distance from %s to %s: %s km", self._home_coordinates,
|
||||
polygon, distance)
|
||||
return distance
|
||||
_LOGGER.warning("Update not successful, no data received from %s",
|
||||
self._feed)
|
||||
# If no events were found due to an error then just set state to
|
||||
# zero.
|
||||
self._state = 0
|
||||
Reference in new issue
Block a user