mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Introduce base class for Trafikverket camera (#100114)
* Introduce base class for Trafikverket camera * fix feedback * Fix feedback
This commit is contained in:
parent
09ad1a9a36
commit
8fe5a5a398
@ -10,12 +10,11 @@ from homeassistant.components.binary_sensor import (
|
|||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import CameraData, TVDataUpdateCoordinator
|
from .coordinator import CameraData, TVDataUpdateCoordinator
|
||||||
|
from .entity import TrafikverketCameraNonCameraEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
@ -51,47 +50,20 @@ async def async_setup_entry(
|
|||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[
|
||||||
TrafikverketCameraBinarySensor(
|
TrafikverketCameraBinarySensor(
|
||||||
coordinator, entry.entry_id, entry.title, BINARY_SENSOR_TYPE
|
coordinator, entry.entry_id, BINARY_SENSOR_TYPE
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TrafikverketCameraBinarySensor(
|
class TrafikverketCameraBinarySensor(
|
||||||
CoordinatorEntity[TVDataUpdateCoordinator], BinarySensorEntity
|
TrafikverketCameraNonCameraEntity, BinarySensorEntity
|
||||||
):
|
):
|
||||||
"""Representation of a Trafikverket Camera binary sensor."""
|
"""Representation of a Trafikverket Camera binary sensor."""
|
||||||
|
|
||||||
entity_description: TVCameraSensorEntityDescription
|
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
|
@callback
|
||||||
def _update_attr(self) -> None:
|
def _update_attr(self) -> None:
|
||||||
"""Update _attr."""
|
"""Update _attr."""
|
||||||
self._attr_is_on = self.entity_description.value_fn(self.coordinator.data)
|
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()
|
|
||||||
|
@ -8,12 +8,11 @@ from homeassistant.components.camera import Camera
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_LOCATION
|
from homeassistant.const import ATTR_LOCATION
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .const import ATTR_DESCRIPTION, ATTR_TYPE, DOMAIN
|
from .const import ATTR_DESCRIPTION, ATTR_TYPE, DOMAIN
|
||||||
from .coordinator import TVDataUpdateCoordinator
|
from .coordinator import TVDataUpdateCoordinator
|
||||||
|
from .entity import TrafikverketCameraEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -29,17 +28,15 @@ async def async_setup_entry(
|
|||||||
[
|
[
|
||||||
TVCamera(
|
TVCamera(
|
||||||
coordinator,
|
coordinator,
|
||||||
entry.title,
|
|
||||||
entry.entry_id,
|
entry.entry_id,
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TVCamera(CoordinatorEntity[TVDataUpdateCoordinator], Camera):
|
class TVCamera(TrafikverketCameraEntity, Camera):
|
||||||
"""Implement Trafikverket camera."""
|
"""Implement Trafikverket camera."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
|
||||||
_attr_name = None
|
_attr_name = None
|
||||||
_attr_translation_key = "tv_camera"
|
_attr_translation_key = "tv_camera"
|
||||||
coordinator: TVDataUpdateCoordinator
|
coordinator: TVDataUpdateCoordinator
|
||||||
@ -47,21 +44,12 @@ class TVCamera(CoordinatorEntity[TVDataUpdateCoordinator], Camera):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: TVDataUpdateCoordinator,
|
coordinator: TVDataUpdateCoordinator,
|
||||||
name: str,
|
|
||||||
entry_id: str,
|
entry_id: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the camera."""
|
"""Initialize the camera."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator, entry_id)
|
||||||
Camera.__init__(self)
|
Camera.__init__(self)
|
||||||
self._attr_unique_id = entry_id
|
self._attr_unique_id = entry_id
|
||||||
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/",
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_camera_image(
|
async def async_camera_image(
|
||||||
self, width: int | None = None, height: int | None = None
|
self, width: int | None = None, height: int | None = None
|
||||||
|
56
homeassistant/components/trafikverket_camera/entity.py
Normal file
56
homeassistant/components/trafikverket_camera/entity.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
"""Base entity for Trafikverket Camera."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||||
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .coordinator import TVDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
|
class TrafikverketCameraEntity(CoordinatorEntity[TVDataUpdateCoordinator]):
|
||||||
|
"""Base entity for Trafikverket Camera."""
|
||||||
|
|
||||||
|
_attr_has_entity_name = True
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: TVDataUpdateCoordinator,
|
||||||
|
entry_id: str,
|
||||||
|
) -> None:
|
||||||
|
"""Initiate Trafikverket Camera Sensor."""
|
||||||
|
super().__init__(coordinator)
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
identifiers={(DOMAIN, entry_id)},
|
||||||
|
entry_type=DeviceEntryType.SERVICE,
|
||||||
|
manufacturer="Trafikverket",
|
||||||
|
model="v1.0",
|
||||||
|
configuration_url="https://api.trafikinfo.trafikverket.se/",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TrafikverketCameraNonCameraEntity(TrafikverketCameraEntity):
|
||||||
|
"""Base entity for Trafikverket Camera but for non camera entities."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: TVDataUpdateCoordinator,
|
||||||
|
entry_id: str,
|
||||||
|
description: EntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initiate Trafikverket Camera Sensor."""
|
||||||
|
super().__init__(coordinator, entry_id)
|
||||||
|
self._attr_unique_id = f"{entry_id}-{description.key}"
|
||||||
|
self.entity_description = description
|
||||||
|
self._update_attr()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _update_attr(self) -> None:
|
||||||
|
"""Update _attr."""
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _handle_coordinator_update(self) -> None:
|
||||||
|
self._update_attr()
|
||||||
|
return super()._handle_coordinator_update()
|
@ -13,13 +13,12 @@ from homeassistant.components.sensor import (
|
|||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import DEGREE
|
from homeassistant.const import DEGREE
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import CameraData, TVDataUpdateCoordinator
|
from .coordinator import CameraData, TVDataUpdateCoordinator
|
||||||
|
from .entity import TrafikverketCameraNonCameraEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 0
|
||||||
|
|
||||||
@ -92,39 +91,15 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
coordinator: TVDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
coordinator: TVDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
TrafikverketCameraSensor(coordinator, entry.entry_id, entry.title, description)
|
TrafikverketCameraSensor(coordinator, entry.entry_id, description)
|
||||||
for description in SENSOR_TYPES
|
for description in SENSOR_TYPES
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TrafikverketCameraSensor(
|
class TrafikverketCameraSensor(TrafikverketCameraNonCameraEntity, SensorEntity):
|
||||||
CoordinatorEntity[TVDataUpdateCoordinator], SensorEntity
|
|
||||||
):
|
|
||||||
"""Representation of a Trafikverket Camera Sensor."""
|
"""Representation of a Trafikverket Camera Sensor."""
|
||||||
|
|
||||||
entity_description: TVCameraSensorEntityDescription
|
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 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
|
@callback
|
||||||
def _update_attr(self) -> None:
|
def _update_attr(self) -> None:
|
||||||
@ -132,8 +107,3 @@ class TrafikverketCameraSensor(
|
|||||||
self._attr_native_value = self.entity_description.value_fn(
|
self._attr_native_value = self.entity_description.value_fn(
|
||||||
self.coordinator.data
|
self.coordinator.data
|
||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
|
||||||
def _handle_coordinator_update(self) -> None:
|
|
||||||
self._update_attr()
|
|
||||||
return super()._handle_coordinator_update()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user