2019-02-19 09:04:14 +00:00
|
|
|
"""Sensor for Crime Reports."""
|
2017-04-07 05:47:03 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
2019-10-20 18:41:11 +00:00
|
|
|
import crimereports
|
2017-04-07 05:47:03 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
ATTR_LATITUDE,
|
|
|
|
ATTR_LONGITUDE,
|
2019-10-20 18:41:11 +00:00
|
|
|
CONF_EXCLUDE,
|
|
|
|
CONF_INCLUDE,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_NAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_RADIUS,
|
|
|
|
LENGTH_KILOMETERS,
|
|
|
|
LENGTH_METERS,
|
|
|
|
)
|
2019-10-20 18:41:11 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-04-07 05:47:03 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.util import slugify
|
|
|
|
from homeassistant.util.distance import convert
|
|
|
|
from homeassistant.util.dt import now
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "crimereports"
|
2017-05-02 16:18:47 +00:00
|
|
|
|
2019-09-03 15:09:59 +00:00
|
|
|
EVENT_INCIDENT = f"{DOMAIN}_incident"
|
2017-05-02 16:18:47 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=30)
|
2017-04-07 05:47:03 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_RADIUS): vol.Coerce(float),
|
|
|
|
vol.Inclusive(CONF_LATITUDE, "coordinates"): cv.latitude,
|
|
|
|
vol.Inclusive(CONF_LONGITUDE, "coordinates"): cv.longitude,
|
|
|
|
vol.Optional(CONF_INCLUDE): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
vol.Optional(CONF_EXCLUDE): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
}
|
|
|
|
)
|
2017-04-07 05:47:03 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Crime Reports platform."""
|
2017-04-07 05:47:03 +00:00
|
|
|
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
|
|
|
|
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
|
2020-04-14 18:38:55 +00:00
|
|
|
name = config[CONF_NAME]
|
|
|
|
radius = config[CONF_RADIUS]
|
2017-05-02 16:18:47 +00:00
|
|
|
include = config.get(CONF_INCLUDE)
|
|
|
|
exclude = config.get(CONF_EXCLUDE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
add_entities(
|
|
|
|
[CrimeReportsSensor(hass, name, latitude, longitude, radius, include, exclude)],
|
|
|
|
True,
|
|
|
|
)
|
2017-04-07 05:47:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CrimeReportsSensor(Entity):
|
2017-10-29 16:28:07 +00:00
|
|
|
"""Representation of a Crime Reports Sensor."""
|
2017-04-07 05:47:03 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(self, hass, name, latitude, longitude, radius, include, exclude):
|
2017-10-29 16:28:07 +00:00
|
|
|
"""Initialize the Crime Reports sensor."""
|
2017-04-07 05:47:03 +00:00
|
|
|
self._hass = hass
|
|
|
|
self._name = name
|
|
|
|
self._include = include
|
|
|
|
self._exclude = exclude
|
|
|
|
radius_kilometers = convert(radius, LENGTH_METERS, LENGTH_KILOMETERS)
|
2017-05-02 16:18:47 +00:00
|
|
|
self._crimereports = crimereports.CrimeReports(
|
2019-07-31 19:25:30 +00:00
|
|
|
(latitude, longitude), radius_kilometers
|
|
|
|
)
|
2017-04-07 05:47:03 +00:00
|
|
|
self._attributes = None
|
|
|
|
self._state = None
|
|
|
|
self._previous_incidents = set()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return self._attributes
|
|
|
|
|
|
|
|
def _incident_event(self, incident):
|
2018-03-15 10:45:54 +00:00
|
|
|
"""Fire if an event occurs."""
|
2017-04-07 05:47:03 +00:00
|
|
|
data = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"type": incident.get("type"),
|
|
|
|
"description": incident.get("friendly_description"),
|
|
|
|
"timestamp": incident.get("timestamp"),
|
|
|
|
"location": incident.get("location"),
|
2017-04-07 05:47:03 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
if incident.get("coordinates"):
|
|
|
|
data.update(
|
|
|
|
{
|
|
|
|
ATTR_LATITUDE: incident.get("coordinates")[0],
|
|
|
|
ATTR_LONGITUDE: incident.get("coordinates")[1],
|
|
|
|
}
|
|
|
|
)
|
2017-04-07 05:47:03 +00:00
|
|
|
self._hass.bus.fire(EVENT_INCIDENT, data)
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update device state."""
|
|
|
|
incident_counts = defaultdict(int)
|
2017-05-02 16:18:47 +00:00
|
|
|
incidents = self._crimereports.get_incidents(
|
2019-07-31 19:25:30 +00:00
|
|
|
now().date(), include=self._include, exclude=self._exclude
|
|
|
|
)
|
2017-04-07 05:47:03 +00:00
|
|
|
fire_events = len(self._previous_incidents) > 0
|
|
|
|
if len(incidents) < len(self._previous_incidents):
|
|
|
|
self._previous_incidents = set()
|
|
|
|
for incident in incidents:
|
2019-07-31 19:25:30 +00:00
|
|
|
incident_type = slugify(incident.get("type"))
|
2017-04-07 05:47:03 +00:00
|
|
|
incident_counts[incident_type] += 1
|
2019-07-31 19:25:30 +00:00
|
|
|
if fire_events and incident.get("id") not in self._previous_incidents:
|
2017-04-07 05:47:03 +00:00
|
|
|
self._incident_event(incident)
|
2019-07-31 19:25:30 +00:00
|
|
|
self._previous_incidents.add(incident.get("id"))
|
|
|
|
self._attributes = {ATTR_ATTRIBUTION: crimereports.ATTRIBUTION}
|
2017-04-07 05:47:03 +00:00
|
|
|
self._attributes.update(incident_counts)
|
|
|
|
self._state = len(incidents)
|