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:
"""Set up a Vivotek IP Camera."""
creds = f"{config[CONF_USERNAME]}:{config[CONF_PASSWORD]}"
args = {
"config": config,
"cam": VivotekCamera(
host=config[CONF_IP_ADDRESS],
port=(443 if config[CONF_SSL] else 80),
verify_ssl=config[CONF_VERIFY_SSL],
usr=config[CONF_USERNAME],
pwd=config[CONF_PASSWORD],
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]}"
),
}
add_entities([VivotekCam(**args)], True)
cam = VivotekCamera(
host=config[CONF_IP_ADDRESS],
port=(443 if config[CONF_SSL] else 80),
verify_ssl=config[CONF_VERIFY_SSL],
usr=config[CONF_USERNAME],
pwd=config[CONF_PASSWORD],
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]}"
)
add_entities([VivotekCam(config, cam, stream_source)], True)
class VivotekCam(Camera):
"""A Vivotek IP camera."""
_attr_brand = DEFAULT_CAMERA_BRAND
_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."""
super().__init__()
self._cam = cam
self._frame_interval = 1 / config[CONF_FRAMERATE]
self._motion_detection_enabled = False
self._model_name = None
self._name = config[CONF_NAME]
self._attr_frame_interval = 1 / config[CONF_FRAMERATE]
self._attr_name = config[CONF_NAME]
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(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
"""Return bytes of camera image."""
return self._cam.snapshot()
@property
def name(self):
"""Return the name of this device."""
return self._name
async def stream_source(self):
async def stream_source(self) -> str:
"""Return the source of the stream."""
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:
"""Disable motion detection in camera."""
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:
"""Enable motion detection in camera."""
response = self._cam.set_param(DEFAULT_EVENT_0_KEY, 1)
self._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
self._attr_motion_detection_enabled = int(response) == 1
def update(self) -> None:
"""Update entity status."""
self._model_name = self._cam.model_name
self._attr_model = self._cam.model_name