2019-04-03 15:40:03 +00:00
|
|
|
"""Sensor platform for Brottsplatskartan information."""
|
2022-01-03 18:18:00 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-12-16 11:21:07 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
import uuid
|
|
|
|
|
2019-10-12 19:45:40 +00:00
|
|
|
import brottsplatskartan
|
2018-12-16 11:21:07 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2018-12-16 11:21:07 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_NAME,
|
|
|
|
)
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2018-12-16 11:21:07 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-12-16 11:21:07 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_AREA = "area"
|
2018-12-18 14:23:53 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "Brottsplatskartan"
|
2018-12-18 14:23:53 +00:00
|
|
|
|
2018-12-16 11:21:07 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=30)
|
|
|
|
|
|
|
|
AREAS = [
|
2019-07-31 19:25:30 +00:00
|
|
|
"Blekinge län",
|
|
|
|
"Dalarnas län",
|
|
|
|
"Gotlands län",
|
|
|
|
"Gävleborgs län",
|
|
|
|
"Hallands län",
|
|
|
|
"Jämtlands län",
|
|
|
|
"Jönköpings län",
|
|
|
|
"Kalmar län",
|
|
|
|
"Kronobergs län",
|
|
|
|
"Norrbottens län",
|
|
|
|
"Skåne län",
|
|
|
|
"Stockholms län",
|
|
|
|
"Södermanlands län",
|
|
|
|
"Uppsala län",
|
|
|
|
"Värmlands län",
|
|
|
|
"Västerbottens län",
|
|
|
|
"Västernorrlands län",
|
|
|
|
"Västmanlands län",
|
|
|
|
"Västra Götalands län",
|
|
|
|
"Örebro län",
|
|
|
|
"Östergötlands län",
|
2018-12-16 11:21:07 +00:00
|
|
|
]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Inclusive(CONF_LATITUDE, "coordinates"): cv.latitude,
|
|
|
|
vol.Inclusive(CONF_LONGITUDE, "coordinates"): cv.longitude,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_AREA, default=[]): vol.All(cv.ensure_list, [vol.In(AREAS)]),
|
|
|
|
}
|
|
|
|
)
|
2018-12-16 11:21:07 +00:00
|
|
|
|
|
|
|
|
2022-01-03 18:18:00 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-12-16 11:21:07 +00:00
|
|
|
"""Set up the Brottsplatskartan platform."""
|
2018-12-18 14:23:53 +00:00
|
|
|
|
|
|
|
area = config.get(CONF_AREA)
|
2018-12-16 11:21:07 +00:00
|
|
|
latitude = config.get(CONF_LATITUDE, hass.config.latitude)
|
|
|
|
longitude = config.get(CONF_LONGITUDE, hass.config.longitude)
|
2020-04-09 07:26:06 +00:00
|
|
|
name = config[CONF_NAME]
|
2018-12-16 11:21:07 +00:00
|
|
|
|
|
|
|
# Every Home Assistant instance should have their own unique
|
|
|
|
# app parameter: https://brottsplatskartan.se/sida/api
|
2020-02-24 16:47:52 +00:00
|
|
|
app = f"ha-{uuid.getnode()}"
|
2018-12-16 11:21:07 +00:00
|
|
|
|
|
|
|
bpk = brottsplatskartan.BrottsplatsKartan(
|
2019-07-31 19:25:30 +00:00
|
|
|
app=app, area=area, latitude=latitude, longitude=longitude
|
|
|
|
)
|
2018-12-16 11:21:07 +00:00
|
|
|
|
2018-12-18 14:23:53 +00:00
|
|
|
add_entities([BrottsplatskartanSensor(bpk, name)], True)
|
2018-12-16 11:21:07 +00:00
|
|
|
|
|
|
|
|
2021-03-22 11:37:16 +00:00
|
|
|
class BrottsplatskartanSensor(SensorEntity):
|
2018-12-16 11:21:07 +00:00
|
|
|
"""Representation of a Brottsplatskartan Sensor."""
|
|
|
|
|
|
|
|
def __init__(self, bpk, name):
|
|
|
|
"""Initialize the Brottsplatskartan sensor."""
|
|
|
|
self._brottsplatskartan = bpk
|
2021-07-20 10:20:56 +00:00
|
|
|
self._attr_name = name
|
2018-12-16 11:21:07 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update device state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-12-16 11:21:07 +00:00
|
|
|
incident_counts = defaultdict(int)
|
|
|
|
incidents = self._brottsplatskartan.get_incidents()
|
|
|
|
|
|
|
|
if incidents is False:
|
|
|
|
_LOGGER.debug("Problems fetching incidents")
|
|
|
|
return
|
|
|
|
|
|
|
|
for incident in incidents:
|
2019-07-31 19:25:30 +00:00
|
|
|
incident_type = incident.get("title_type")
|
2018-12-16 11:21:07 +00:00
|
|
|
incident_counts[incident_type] += 1
|
|
|
|
|
2021-07-20 10:20:56 +00:00
|
|
|
self._attr_extra_state_attributes = {
|
|
|
|
ATTR_ATTRIBUTION: brottsplatskartan.ATTRIBUTION
|
|
|
|
}
|
|
|
|
self._attr_extra_state_attributes.update(incident_counts)
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_value = len(incidents)
|