mirror of https://github.com/home-assistant/core
Add binary sensors to Trafikverket Camera (#100082)
This commit is contained in:
parent
0fae65abde
commit
954293f77e
|
@ -0,0 +1,97 @@
|
|||
"""Binary sensor platform for Trafikverket Camera integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import CameraData, TVDataUpdateCoordinator
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
@dataclass
|
||||
class DeviceBaseEntityDescriptionMixin:
|
||||
"""Mixin for required Trafikverket Camera base description keys."""
|
||||
|
||||
value_fn: Callable[[CameraData], bool | None]
|
||||
|
||||
|
||||
@dataclass
|
||||
class TVCameraSensorEntityDescription(
|
||||
BinarySensorEntityDescription, DeviceBaseEntityDescriptionMixin
|
||||
):
|
||||
"""Describes Trafikverket Camera binary sensor entity."""
|
||||
|
||||
|
||||
BINARY_SENSOR_TYPE = TVCameraSensorEntityDescription(
|
||||
key="active",
|
||||
translation_key="active",
|
||||
icon="mdi:camera-outline",
|
||||
value_fn=lambda data: data.data.active,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up Trafikverket Camera binary sensor platform."""
|
||||
|
||||
coordinator: TVDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities(
|
||||
[
|
||||
TrafikverketCameraBinarySensor(
|
||||
coordinator, entry.entry_id, entry.title, BINARY_SENSOR_TYPE
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class TrafikverketCameraBinarySensor(
|
||||
CoordinatorEntity[TVDataUpdateCoordinator], BinarySensorEntity
|
||||
):
|
||||
"""Representation of a Trafikverket Camera binary sensor."""
|
||||
|
||||
entity_description: TVCameraSensorEntityDescription
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: TVDataUpdateCoordinator,
|
||||
entry_id: str,
|
||||
name: str,
|
||||
entity_description: TVCameraSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initiate Trafikverket Camera Binary sensor."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = entity_description
|
||||
self._attr_unique_id = f"{entry_id}-{entity_description.key}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
identifiers={(DOMAIN, entry_id)},
|
||||
manufacturer="Trafikverket",
|
||||
model="v1.0",
|
||||
name=name,
|
||||
configuration_url="https://api.trafikinfo.trafikverket.se/",
|
||||
)
|
||||
self._update_attr()
|
||||
|
||||
@callback
|
||||
def _update_attr(self) -> None:
|
||||
"""Update _attr."""
|
||||
self._attr_is_on = self.entity_description.value_fn(self.coordinator.data)
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
self._update_attr()
|
||||
return super()._handle_coordinator_update()
|
|
@ -3,7 +3,7 @@ from homeassistant.const import Platform
|
|||
|
||||
DOMAIN = "trafikverket_camera"
|
||||
CONF_LOCATION = "location"
|
||||
PLATFORMS = [Platform.CAMERA, Platform.SENSOR]
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.CAMERA, Platform.SENSOR]
|
||||
ATTRIBUTION = "Data provided by Trafikverket"
|
||||
|
||||
ATTR_DESCRIPTION = "description"
|
||||
|
|
|
@ -47,6 +47,11 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"binary_sensor": {
|
||||
"active": {
|
||||
"name": "Active"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"direction": {
|
||||
"name": "Direction"
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
"""The test for the Trafikverket binary sensor platform."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pytrafikverket.trafikverket_camera import CameraInfo
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import STATE_ON
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
async def test_sensor(
|
||||
hass: HomeAssistant,
|
||||
entity_registry_enabled_by_default: None,
|
||||
load_int: ConfigEntry,
|
||||
get_camera: CameraInfo,
|
||||
) -> None:
|
||||
"""Test the Trafikverket Camera binary sensor."""
|
||||
|
||||
state = hass.states.get("binary_sensor.test_location_active")
|
||||
assert state.state == STATE_ON
|
|
@ -38,7 +38,7 @@ async def test_exclude_attributes(
|
|||
None,
|
||||
hass.states.async_entity_ids(),
|
||||
)
|
||||
assert len(states) == 7
|
||||
assert len(states) == 8
|
||||
assert states.get("camera.test_location")
|
||||
for entity_states in states.values():
|
||||
for state in entity_states:
|
||||
|
|
Loading…
Reference in New Issue