Use shorthand attributes in vivotek camera (#145275)

This commit is contained in:
epenet 2025-05-20 10:15:04 +02:00 committed by GitHub
parent 611d5be40a
commit 0cd93e7e65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -62,85 +62,58 @@ def setup_platform(
) -> None: ) -> None:
"""Set up a Vivotek IP Camera.""" """Set up a Vivotek IP Camera."""
creds = f"{config[CONF_USERNAME]}:{config[CONF_PASSWORD]}" creds = f"{config[CONF_USERNAME]}:{config[CONF_PASSWORD]}"
args = { cam = VivotekCamera(
"config": config, host=config[CONF_IP_ADDRESS],
"cam": VivotekCamera( port=(443 if config[CONF_SSL] else 80),
host=config[CONF_IP_ADDRESS], verify_ssl=config[CONF_VERIFY_SSL],
port=(443 if config[CONF_SSL] else 80), usr=config[CONF_USERNAME],
verify_ssl=config[CONF_VERIFY_SSL], pwd=config[CONF_PASSWORD],
usr=config[CONF_USERNAME], digest_auth=config[CONF_AUTHENTICATION] == HTTP_DIGEST_AUTHENTICATION,
pwd=config[CONF_PASSWORD], sec_lvl=config[CONF_SECURITY_LEVEL],
digest_auth=config[CONF_AUTHENTICATION] == HTTP_DIGEST_AUTHENTICATION, )
sec_lvl=config[CONF_SECURITY_LEVEL], stream_source = (
), f"rtsp://{creds}@{config[CONF_IP_ADDRESS]}:554/{config[CONF_STREAM_PATH]}"
"stream_source": ( )
f"rtsp://{creds}@{config[CONF_IP_ADDRESS]}:554/{config[CONF_STREAM_PATH]}" add_entities([VivotekCam(config, cam, stream_source)], True)
),
}
add_entities([VivotekCam(**args)], True)
class VivotekCam(Camera): class VivotekCam(Camera):
"""A Vivotek IP camera.""" """A Vivotek IP camera."""
_attr_brand = DEFAULT_CAMERA_BRAND
_attr_supported_features = CameraEntityFeature.STREAM _attr_supported_features = CameraEntityFeature.STREAM
def __init__(self, config, cam, stream_source): def __init__(
self, config: ConfigType, cam: VivotekCamera, stream_source: str
) -> None:
"""Initialize a Vivotek camera.""" """Initialize a Vivotek camera."""
super().__init__() super().__init__()
self._cam = cam self._cam = cam
self._frame_interval = 1 / config[CONF_FRAMERATE] self._attr_frame_interval = 1 / config[CONF_FRAMERATE]
self._motion_detection_enabled = False self._attr_name = config[CONF_NAME]
self._model_name = None
self._name = config[CONF_NAME]
self._stream_source = stream_source self._stream_source = stream_source
@property
def frame_interval(self):
"""Return the interval between frames of the mjpeg stream."""
return self._frame_interval
def camera_image( def camera_image(
self, width: int | None = None, height: int | None = None self, width: int | None = None, height: int | None = None
) -> bytes | None: ) -> bytes | None:
"""Return bytes of camera image.""" """Return bytes of camera image."""
return self._cam.snapshot() return self._cam.snapshot()
@property async def stream_source(self) -> str:
def name(self):
"""Return the name of this device."""
return self._name
async def stream_source(self):
"""Return the source of the stream.""" """Return the source of the stream."""
return self._stream_source return self._stream_source
@property
def motion_detection_enabled(self):
"""Return the camera motion detection status."""
return self._motion_detection_enabled
def disable_motion_detection(self) -> None: def disable_motion_detection(self) -> None:
"""Disable motion detection in camera.""" """Disable motion detection in camera."""
response = self._cam.set_param(DEFAULT_EVENT_0_KEY, 0) response = self._cam.set_param(DEFAULT_EVENT_0_KEY, 0)
self._motion_detection_enabled = int(response) == 1 self._attr_motion_detection_enabled = int(response) == 1
def enable_motion_detection(self) -> None: def enable_motion_detection(self) -> None:
"""Enable motion detection in camera.""" """Enable motion detection in camera."""
response = self._cam.set_param(DEFAULT_EVENT_0_KEY, 1) response = self._cam.set_param(DEFAULT_EVENT_0_KEY, 1)
self._motion_detection_enabled = int(response) == 1 self._attr_motion_detection_enabled = int(response) == 1
@property
def brand(self):
"""Return the camera brand."""
return DEFAULT_CAMERA_BRAND
@property
def model(self):
"""Return the camera model."""
return self._model_name
def update(self) -> None: def update(self) -> None:
"""Update entity status.""" """Update entity status."""
self._model_name = self._cam.model_name self._attr_model = self._cam.model_name