Use the new EntityDescription for motionEye switches (#53536)
This commit is contained in:
parent
1968b95829
commit
1aec069f3a
2 changed files with 39 additions and 28 deletions
|
@ -52,7 +52,7 @@ from homeassistant.helpers.dispatcher import (
|
|||
async_dispatcher_connect,
|
||||
async_dispatcher_send,
|
||||
)
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.entity import DeviceInfo, EntityDescription
|
||||
from homeassistant.helpers.network import get_url
|
||||
from homeassistant.helpers.update_coordinator import (
|
||||
CoordinatorEntity,
|
||||
|
@ -442,7 +442,7 @@ class MotionEyeEntity(CoordinatorEntity):
|
|||
client: MotionEyeClient,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
options: MappingProxyType[str, Any],
|
||||
enabled_by_default: bool = True,
|
||||
entity_description: EntityDescription = None,
|
||||
) -> None:
|
||||
"""Initialize a motionEye entity."""
|
||||
self._camera_id = camera[KEY_ID]
|
||||
|
@ -457,14 +457,10 @@ class MotionEyeEntity(CoordinatorEntity):
|
|||
self._client = client
|
||||
self._camera: dict[str, Any] | None = camera
|
||||
self._options = options
|
||||
self._enabled_by_default = enabled_by_default
|
||||
if entity_description is not None:
|
||||
self.entity_description = entity_description
|
||||
super().__init__(coordinator)
|
||||
|
||||
@property
|
||||
def entity_registry_enabled_default(self) -> bool:
|
||||
"""Whether or not the entity is enabled by default."""
|
||||
return self._enabled_by_default
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return a unique id for this instance."""
|
||||
|
|
|
@ -18,6 +18,7 @@ from motioneye_client.const import (
|
|||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
|
@ -25,12 +26,30 @@ from . import MotionEyeEntity, get_camera_from_cameras, listen_for_new_cameras
|
|||
from .const import CONF_CLIENT, CONF_COORDINATOR, DOMAIN, TYPE_MOTIONEYE_SWITCH_BASE
|
||||
|
||||
MOTIONEYE_SWITCHES = [
|
||||
(KEY_MOTION_DETECTION, "Motion Detection", True),
|
||||
(KEY_TEXT_OVERLAY, "Text Overlay", False),
|
||||
(KEY_VIDEO_STREAMING, "Video Streaming", False),
|
||||
(KEY_STILL_IMAGES, "Still Images", True),
|
||||
(KEY_MOVIES, "Movies", True),
|
||||
(KEY_UPLOAD_ENABLED, "Upload Enabled", False),
|
||||
EntityDescription(
|
||||
key=KEY_MOTION_DETECTION,
|
||||
name="Motion Detection",
|
||||
entity_registry_enabled_default=True,
|
||||
),
|
||||
EntityDescription(
|
||||
key=KEY_TEXT_OVERLAY, name="Text Overlay", entity_registry_enabled_default=False
|
||||
),
|
||||
EntityDescription(
|
||||
key=KEY_VIDEO_STREAMING,
|
||||
name="Video Streaming",
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
EntityDescription(
|
||||
key=KEY_STILL_IMAGES, name="Still Images", entity_registry_enabled_default=True
|
||||
),
|
||||
EntityDescription(
|
||||
key=KEY_MOVIES, name="Movies", entity_registry_enabled_default=True
|
||||
),
|
||||
EntityDescription(
|
||||
key=KEY_UPLOAD_ENABLED,
|
||||
name="Upload Enabled",
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
|
@ -48,14 +67,12 @@ async def async_setup_entry(
|
|||
MotionEyeSwitch(
|
||||
entry.entry_id,
|
||||
camera,
|
||||
switch_key,
|
||||
switch_key_friendly_name,
|
||||
entry_data[CONF_CLIENT],
|
||||
entry_data[CONF_COORDINATOR],
|
||||
entry.options,
|
||||
enabled,
|
||||
entity_description,
|
||||
)
|
||||
for switch_key, switch_key_friendly_name, enabled in MOTIONEYE_SWITCHES
|
||||
for entity_description in MOTIONEYE_SWITCHES
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -69,36 +86,34 @@ class MotionEyeSwitch(MotionEyeEntity, SwitchEntity):
|
|||
self,
|
||||
config_entry_id: str,
|
||||
camera: dict[str, Any],
|
||||
switch_key: str,
|
||||
switch_key_friendly_name: str,
|
||||
client: MotionEyeClient,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
options: MappingProxyType[str, str],
|
||||
enabled_by_default: bool,
|
||||
entity_description: EntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the switch."""
|
||||
self._switch_key = switch_key
|
||||
self._switch_key_friendly_name = switch_key_friendly_name
|
||||
super().__init__(
|
||||
config_entry_id,
|
||||
f"{TYPE_MOTIONEYE_SWITCH_BASE}_{switch_key}",
|
||||
f"{TYPE_MOTIONEYE_SWITCH_BASE}_{entity_description.key}",
|
||||
camera,
|
||||
client,
|
||||
coordinator,
|
||||
options,
|
||||
enabled_by_default,
|
||||
entity_description,
|
||||
)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the switch."""
|
||||
camera_prepend = f"{self._camera[KEY_NAME]} " if self._camera else ""
|
||||
return f"{camera_prepend}{self._switch_key_friendly_name}"
|
||||
return f"{camera_prepend}{self.entity_description.name}"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if the switch is on."""
|
||||
return bool(self._camera and self._camera.get(self._switch_key, False))
|
||||
return bool(
|
||||
self._camera and self._camera.get(self.entity_description.key, False)
|
||||
)
|
||||
|
||||
async def _async_send_set_camera(self, value: bool) -> None:
|
||||
"""Set a switch value."""
|
||||
|
@ -107,7 +122,7 @@ class MotionEyeSwitch(MotionEyeEntity, SwitchEntity):
|
|||
# stale configuration.
|
||||
camera = await self._client.async_get_camera(self._camera_id)
|
||||
if camera:
|
||||
camera[self._switch_key] = value
|
||||
camera[self.entity_description.key] = value
|
||||
await self._client.async_set_camera(self._camera_id, camera)
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
|
|
Loading…
Add table
Reference in a new issue