Move motioneye base entity to separate module (#126495)
This commit is contained in:
parent
da3f18839a
commit
1858c64e5f
6 changed files with 81 additions and 72 deletions
|
@ -8,7 +8,6 @@ from http import HTTPStatus
|
|||
import json
|
||||
import logging
|
||||
import os
|
||||
from types import MappingProxyType
|
||||
from typing import Any
|
||||
from urllib.parse import urlencode, urljoin
|
||||
|
||||
|
@ -52,18 +51,12 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.network import NoURLAvailableError, get_url
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
UpdateFailed,
|
||||
)
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import (
|
||||
ATTR_EVENT_TYPE,
|
||||
|
@ -125,13 +118,6 @@ def split_motioneye_device_identifier(
|
|||
return (DOMAIN, config_id, camera_id)
|
||||
|
||||
|
||||
def get_motioneye_entity_unique_id(
|
||||
config_entry_id: str, camera_id: int, entity_type: str
|
||||
) -> str:
|
||||
"""Get the unique_id for a motionEye entity."""
|
||||
return f"{config_entry_id}_{camera_id}_{entity_type}"
|
||||
|
||||
|
||||
def get_camera_from_cameras(
|
||||
camera_id: int, data: dict[str, Any] | None
|
||||
) -> dict[str, Any] | None:
|
||||
|
@ -530,51 +516,3 @@ def get_media_url(
|
|||
return client.get_image_url(camera_id, path)
|
||||
return client.get_movie_url(camera_id, path)
|
||||
return None
|
||||
|
||||
|
||||
class MotionEyeEntity(CoordinatorEntity):
|
||||
"""Base class for motionEye entities."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry_id: str,
|
||||
type_name: str,
|
||||
camera: dict[str, Any],
|
||||
client: MotionEyeClient,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
options: MappingProxyType[str, Any],
|
||||
entity_description: EntityDescription | None = None,
|
||||
) -> None:
|
||||
"""Initialize a motionEye entity."""
|
||||
self._camera_id = camera[KEY_ID]
|
||||
self._device_identifier = get_motioneye_device_identifier(
|
||||
config_entry_id, self._camera_id
|
||||
)
|
||||
self._unique_id = get_motioneye_entity_unique_id(
|
||||
config_entry_id,
|
||||
self._camera_id,
|
||||
type_name,
|
||||
)
|
||||
self._client = client
|
||||
self._camera: dict[str, Any] | None = camera
|
||||
self._options = options
|
||||
if entity_description is not None:
|
||||
self.entity_description = entity_description
|
||||
super().__init__(coordinator)
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique id for this instance."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return the device information."""
|
||||
return DeviceInfo(identifiers={self._device_identifier})
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return self._camera is not None and super().available
|
||||
|
|
|
@ -45,12 +45,7 @@ from homeassistant.helpers import config_validation as cv, entity_platform
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from . import (
|
||||
MotionEyeEntity,
|
||||
get_camera_from_cameras,
|
||||
is_acceptable_camera,
|
||||
listen_for_new_cameras,
|
||||
)
|
||||
from . import get_camera_from_cameras, is_acceptable_camera, listen_for_new_cameras
|
||||
from .const import (
|
||||
CONF_ACTION,
|
||||
CONF_CLIENT,
|
||||
|
@ -65,6 +60,7 @@ from .const import (
|
|||
SERVICE_SNAPSHOT,
|
||||
TYPE_MOTIONEYE_MJPEG_CAMERA,
|
||||
)
|
||||
from .entity import MotionEyeEntity
|
||||
|
||||
PLATFORMS = [Platform.CAMERA]
|
||||
|
||||
|
|
73
homeassistant/components/motioneye/entity.py
Normal file
73
homeassistant/components/motioneye/entity.py
Normal file
|
@ -0,0 +1,73 @@
|
|||
"""The motionEye integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from types import MappingProxyType
|
||||
from typing import Any
|
||||
|
||||
from motioneye_client.client import MotionEyeClient
|
||||
from motioneye_client.const import KEY_ID
|
||||
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
DataUpdateCoordinator,
|
||||
)
|
||||
|
||||
from . import get_motioneye_device_identifier
|
||||
|
||||
|
||||
def get_motioneye_entity_unique_id(
|
||||
config_entry_id: str, camera_id: int, entity_type: str
|
||||
) -> str:
|
||||
"""Get the unique_id for a motionEye entity."""
|
||||
return f"{config_entry_id}_{camera_id}_{entity_type}"
|
||||
|
||||
|
||||
class MotionEyeEntity(CoordinatorEntity):
|
||||
"""Base class for motionEye entities."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config_entry_id: str,
|
||||
type_name: str,
|
||||
camera: dict[str, Any],
|
||||
client: MotionEyeClient,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
options: MappingProxyType[str, Any],
|
||||
entity_description: EntityDescription | None = None,
|
||||
) -> None:
|
||||
"""Initialize a motionEye entity."""
|
||||
self._camera_id = camera[KEY_ID]
|
||||
self._device_identifier = get_motioneye_device_identifier(
|
||||
config_entry_id, self._camera_id
|
||||
)
|
||||
self._unique_id = get_motioneye_entity_unique_id(
|
||||
config_entry_id,
|
||||
self._camera_id,
|
||||
type_name,
|
||||
)
|
||||
self._client = client
|
||||
self._camera: dict[str, Any] | None = camera
|
||||
self._options = options
|
||||
if entity_description is not None:
|
||||
self.entity_description = entity_description
|
||||
super().__init__(coordinator)
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique id for this instance."""
|
||||
return self._unique_id
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return the device information."""
|
||||
return DeviceInfo(identifiers={self._device_identifier})
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return self._camera is not None and super().available
|
|
@ -16,8 +16,9 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from . import MotionEyeEntity, get_camera_from_cameras, listen_for_new_cameras
|
||||
from . import get_camera_from_cameras, listen_for_new_cameras
|
||||
from .const import CONF_CLIENT, CONF_COORDINATOR, DOMAIN, TYPE_MOTIONEYE_ACTION_SENSOR
|
||||
from .entity import MotionEyeEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
|
|
@ -22,8 +22,9 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from . import MotionEyeEntity, get_camera_from_cameras, listen_for_new_cameras
|
||||
from . import get_camera_from_cameras, listen_for_new_cameras
|
||||
from .const import CONF_CLIENT, CONF_COORDINATOR, DOMAIN, TYPE_MOTIONEYE_SWITCH_BASE
|
||||
from .entity import MotionEyeEntity
|
||||
|
||||
MOTIONEYE_SWITCHES = [
|
||||
SwitchEntityDescription(
|
||||
|
|
|
@ -7,8 +7,8 @@ from unittest.mock import AsyncMock, Mock, patch
|
|||
|
||||
from motioneye_client.const import DEFAULT_PORT
|
||||
|
||||
from homeassistant.components.motioneye import get_motioneye_entity_unique_id
|
||||
from homeassistant.components.motioneye.const import DOMAIN
|
||||
from homeassistant.components.motioneye.entity import get_motioneye_entity_unique_id
|
||||
from homeassistant.config import async_process_ha_core_config
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_URL
|
||||
|
|
Loading…
Add table
Reference in a new issue