diff --git a/linuxpy/video/device.py b/linuxpy/video/device.py index 75b1f2d..882654e 100644 --- a/linuxpy/video/device.py +++ b/linuxpy/video/device.py @@ -1059,20 +1059,27 @@ def __getattr__(self, key): raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{key}'") def __setattr__(self, key, value): + self._init_if_needed() self[key] = value def __delattr__(self, key): + self._init_if_needed() try: del self[key] except KeyError as error: raise AttributeError(key) from error def __missing__(self, key): + self._init_if_needed() for v in self.values(): if isinstance(v, BaseControl) and (v.config_name == key): return v raise KeyError(key) + def values(self): + self._init_if_needed() + return super().values() + def used_classes(self): class_map = {v.control_class.id: v.control_class for v in self.values() if isinstance(v, BaseControl)} return list(class_map.values()) diff --git a/tests/test_video.py b/tests/test_video.py index 1d046c8..9f3fef6 100644 --- a/tests/test_video.py +++ b/tests/test_video.py @@ -31,6 +31,7 @@ Device, Memory, PixelFormat, + Priority, VideoCapture, VideoOutput, iter_devices, @@ -702,12 +703,14 @@ def _(): def _(): with Device(VIVID_TEST_DEVICES[0]) as device: controls = device.controls + + # Brightness assert controls.brightness is controls["brightness"] current_value = controls.brightness.value assert 0 <= current_value < 250 try: - controls.brightness.value = not current_value - assert controls.brightness.value == (not current_value) + controls.brightness.value = 100 + assert controls.brightness.value == 100 finally: controls.brightness.value = current_value @@ -719,8 +722,59 @@ def _(): assert controls.brightness in controls.with_class(ControlClass.USER) assert "" in repr(controls.string) + + # 2 element array + assert controls.s32_2_element_array is controls["s32_2_element_array"] + current_value = controls.s32_2_element_array.value + try: + controls.s32_2_element_array.value = [5, -5] + assert controls.s32_2_element_array.value[:] == [5, -5] + finally: + controls.s32_2_element_array.value = current_value + + assert "" in repr(controls.s32_2_element_array) + + # Unknown with raises(KeyError): _ = list(controls.with_class("unknown class")) with raises(ValueError): _ = list(controls.with_class(55)) + + +@skip(when=not is_vivid_prepared(), reason="vivid is not prepared") +@skip(when=not is_vivid_installed(), reason="vivid is not installed") +@test("info with vivid") +def _(): + with Device(VIVID_TEST_DEVICES[0]) as device: + text = repr(device.info) + assert "driver = " in text + + assert len(device.info.frame_sizes) > 10 + assert len(device.info.formats) > 10 + + inputs = device.info.inputs + assert len(inputs) > 0 + active_input = device.get_input() + assert active_input in {inp.index for inp in inputs} + try: + device.set_input(inputs[-1].index) + assert device.get_input() == inputs[-1].index + finally: + device.set_input(active_input) + + outputs = device.info.outputs + assert len(outputs) >= 0 + + assert isinstance(device.get_priority(), Priority)