2023-08-24 08:39:22 +00:00
|
|
|
"""Camera for the Trafikverket Camera integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Mapping
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.components.camera import Camera
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import ATTR_LOCATION
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import ATTR_DESCRIPTION, ATTR_TYPE, DOMAIN
|
|
|
|
from .coordinator import TVDataUpdateCoordinator
|
2023-09-12 18:48:47 +00:00
|
|
|
from .entity import TrafikverketCameraEntity
|
2023-08-24 08:39:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up a Trafikverket Camera."""
|
|
|
|
|
|
|
|
coordinator: TVDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
TVCamera(
|
|
|
|
coordinator,
|
|
|
|
entry.entry_id,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-12 18:48:47 +00:00
|
|
|
class TVCamera(TrafikverketCameraEntity, Camera):
|
2023-08-24 08:39:22 +00:00
|
|
|
"""Implement Trafikverket camera."""
|
|
|
|
|
2023-09-21 13:02:47 +00:00
|
|
|
_unrecorded_attributes = frozenset({ATTR_DESCRIPTION, ATTR_LOCATION})
|
|
|
|
|
2023-08-24 08:39:22 +00:00
|
|
|
_attr_name = None
|
|
|
|
_attr_translation_key = "tv_camera"
|
|
|
|
coordinator: TVDataUpdateCoordinator
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: TVDataUpdateCoordinator,
|
|
|
|
entry_id: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the camera."""
|
2023-09-12 18:48:47 +00:00
|
|
|
super().__init__(coordinator, entry_id)
|
2023-08-24 08:39:22 +00:00
|
|
|
Camera.__init__(self)
|
|
|
|
self._attr_unique_id = entry_id
|
|
|
|
|
|
|
|
async def async_camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
|
|
|
"""Return camera picture."""
|
|
|
|
return self.coordinator.data.image
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return camera on."""
|
|
|
|
return self.coordinator.data.data.active is True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
|
|
|
"""Return additional attributes."""
|
|
|
|
return {
|
|
|
|
ATTR_DESCRIPTION: self.coordinator.data.data.description,
|
|
|
|
ATTR_LOCATION: self.coordinator.data.data.location,
|
|
|
|
ATTR_TYPE: self.coordinator.data.data.camera_type,
|
|
|
|
}
|