1 """Sensor platform for Brottsplatskartan information."""
3 from __future__
import annotations
5 from collections
import defaultdict
6 from datetime
import timedelta
7 from typing
import Literal
9 from brottsplatskartan
import ATTRIBUTION, BrottsplatsKartan
18 from .const
import CONF_APP_ID, CONF_AREA, DOMAIN, LOGGER
24 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
26 """Set up the Brottsplatskartan sensor entry."""
28 area = entry.data.get(CONF_AREA)
29 latitude = entry.data.get(CONF_LATITUDE)
30 longitude = entry.data.get(CONF_LONGITUDE)
31 app = entry.data[CONF_APP_ID]
34 bpk = BrottsplatsKartan(
35 app=app, areas=[area]
if area
else None, latitude=latitude, longitude=longitude
42 """Representation of a Brottsplatskartan Sensor."""
44 _attr_attribution = ATTRIBUTION
45 _attr_has_entity_name =
True
49 self, bpk: BrottsplatsKartan, name: str, entry_id: str, area: str |
None
51 """Initialize the Brottsplatskartan sensor."""
56 entry_type=DeviceEntryType.SERVICE,
57 identifiers={(DOMAIN, entry_id)},
58 manufacturer=
"Brottsplatskartan",
63 """Update device state."""
65 incident_counts: defaultdict[str, int] = defaultdict(int)
66 get_incidents: dict[str, list] | Literal[
False] = (
70 if get_incidents
is False:
71 LOGGER.debug(
"Problems fetching incidents")
75 incidents = get_incidents.get(self.
_area_area)
or []
77 incidents = get_incidents.get(
"latlng")
or []
79 for incident
in incidents:
80 if (incident_type := incident.get(
"title_type"))
is not None:
81 incident_counts[incident_type] += 1
None __init__(self, BrottsplatsKartan bpk, str name, str entry_id, str|None area)
_attr_extra_state_attributes
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)