2019-04-03 15:40:03 +00:00
|
|
|
"""Sensor platform for Brottsplatskartan information."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
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
|
2023-12-14 22:36:12 +00:00
|
|
|
from typing import Literal
|
2018-12-16 11:21:07 +00:00
|
|
|
|
2023-04-12 09:05:24 +00:00
|
|
|
from brottsplatskartan import ATTRIBUTION, BrottsplatsKartan
|
2018-12-16 11:21:07 +00:00
|
|
|
|
2023-10-02 09:00:41 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
|
|
|
from homeassistant.core import HomeAssistant
|
2023-08-11 02:04:26 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
2022-01-03 18:18:00 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-12-16 11:21:07 +00:00
|
|
|
|
2023-10-02 09:00:41 +00:00
|
|
|
from .const import CONF_APP_ID, CONF_AREA, DOMAIN, LOGGER
|
2018-12-18 14:23:53 +00:00
|
|
|
|
2018-12-16 11:21:07 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=30)
|
|
|
|
|
2023-04-12 09:05:24 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up the Brottsplatskartan sensor entry."""
|
|
|
|
|
|
|
|
area = entry.data.get(CONF_AREA)
|
|
|
|
latitude = entry.data.get(CONF_LATITUDE)
|
|
|
|
longitude = entry.data.get(CONF_LONGITUDE)
|
|
|
|
app = entry.data[CONF_APP_ID]
|
|
|
|
name = entry.title
|
|
|
|
|
2023-12-14 22:36:12 +00:00
|
|
|
bpk = BrottsplatsKartan(
|
|
|
|
app=app, areas=[area] if area else None, latitude=latitude, longitude=longitude
|
|
|
|
)
|
2023-04-12 09:05:24 +00:00
|
|
|
|
2023-12-14 22:36:12 +00:00
|
|
|
async_add_entities([BrottsplatskartanSensor(bpk, name, entry.entry_id, area)], 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."""
|
|
|
|
|
2023-04-12 09:05:24 +00:00
|
|
|
_attr_attribution = ATTRIBUTION
|
|
|
|
_attr_has_entity_name = True
|
2023-06-30 15:33:50 +00:00
|
|
|
_attr_name = None
|
2022-08-11 01:24:12 +00:00
|
|
|
|
2023-12-14 22:36:12 +00:00
|
|
|
def __init__(
|
|
|
|
self, bpk: BrottsplatsKartan, name: str, entry_id: str, area: str | None
|
|
|
|
) -> None:
|
2018-12-16 11:21:07 +00:00
|
|
|
"""Initialize the Brottsplatskartan sensor."""
|
|
|
|
self._brottsplatskartan = bpk
|
2023-12-14 22:36:12 +00:00
|
|
|
self._area = area
|
2023-04-12 09:05:24 +00:00
|
|
|
self._attr_unique_id = entry_id
|
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
identifiers={(DOMAIN, entry_id)},
|
|
|
|
manufacturer="Brottsplatskartan",
|
|
|
|
name=name,
|
|
|
|
)
|
2018-12-16 11:21:07 +00:00
|
|
|
|
2022-08-19 08:30:34 +00:00
|
|
|
def update(self) -> None:
|
2018-12-16 11:21:07 +00:00
|
|
|
"""Update device state."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2022-08-19 08:30:34 +00:00
|
|
|
incident_counts: defaultdict[str, int] = defaultdict(int)
|
2024-03-25 23:02:16 +00:00
|
|
|
get_incidents: dict[str, list] | Literal[False] = (
|
|
|
|
self._brottsplatskartan.get_incidents()
|
|
|
|
)
|
2018-12-16 11:21:07 +00:00
|
|
|
|
2023-12-14 22:36:12 +00:00
|
|
|
if get_incidents is False:
|
2023-03-23 10:54:15 +00:00
|
|
|
LOGGER.debug("Problems fetching incidents")
|
2018-12-16 11:21:07 +00:00
|
|
|
return
|
|
|
|
|
2023-12-14 22:36:12 +00:00
|
|
|
if self._area:
|
|
|
|
incidents = get_incidents.get(self._area) or []
|
|
|
|
else:
|
|
|
|
incidents = get_incidents.get("latlng") or []
|
|
|
|
|
2018-12-16 11:21:07 +00:00
|
|
|
for incident in incidents:
|
2022-08-19 10:09:58 +00:00
|
|
|
if (incident_type := incident.get("title_type")) is not None:
|
|
|
|
incident_counts[incident_type] += 1
|
2018-12-16 11:21:07 +00:00
|
|
|
|
2022-08-11 01:24:12 +00:00
|
|
|
self._attr_extra_state_attributes = incident_counts
|
2021-08-11 08:45:05 +00:00
|
|
|
self._attr_native_value = len(incidents)
|