From 8dfa0915356946968380674c3db20edd2801de42 Mon Sep 17 00:00:00 2001 From: Marc-Olivier Arsenault Date: Sun, 26 Nov 2023 17:15:10 -0500 Subject: [PATCH 1/4] add hardcoded webcam --- custom_components/moonraker/camera.py | 37 +++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/custom_components/moonraker/camera.py b/custom_components/moonraker/camera.py index 8f8e48a..19bd58c 100755 --- a/custom_components/moonraker/camera.py +++ b/custom_components/moonraker/camera.py @@ -15,6 +15,19 @@ _LOGGER = logging.getLogger(__name__) +hardcoded_camera = { + "name": "webcam", + "location": "printer", + "service": "mjpegstreamer-adaptive", + "target_fps": "15", + "stream_url": "/webcam/?action=stream", + "snapshot_url": "/webcam/?action=snapshot", + "flip_horizontal": False, + "flip_vertical": False, + "rotation": 0, + "source": "database", +} + async def async_setup_entry( hass: HomeAssistant, @@ -24,12 +37,26 @@ async def async_setup_entry( """Set up the available Moonraker camera.""" coordinator = hass.data[DOMAIN][config_entry.entry_id] - cameras = await coordinator.async_fetch_data(METHODS.SERVER_WEBCAMS_LIST) + camera_cnt = 0 - for camera_id, camera in enumerate(cameras["webcams"]): - async_add_entities( - [MoonrakerCamera(config_entry, coordinator, camera, camera_id)] - ) + try: + cameras = await coordinator.async_fetch_data(METHODS.SERVER_WEBCAMS_LIST) + for camera_id, camera in enumerate(cameras["webcams"]): + async_add_entities( + [MoonrakerCamera(config_entry, coordinator, camera, camera_id)] + ) + camera_cnt += 1 + except Exception: + _LOGGER.info("Could not add any cameras from the API list") + + if camera_cnt == 0: + try: + _LOGGER.info("No Camera in the list, trying hardcoded") + async_add_entities( + [MoonrakerCamera(config_entry, coordinator, hardcoded_camera, 0)] + ) + except Exception: + _LOGGER.info("Could not add hardcoded camera") async_add_entities( [ From 201abc75bdd8ab167427a4328b26abfee2f06e9d Mon Sep 17 00:00:00 2001 From: Marc-Olivier Arsenault Date: Sun, 26 Nov 2023 17:41:40 -0500 Subject: [PATCH 2/4] add default test --- tests/test_camera.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/test_camera.py b/tests/test_camera.py index 2b68089..8e3df85 100755 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -114,6 +114,21 @@ async def test_setup_thumbnail_camera(hass, get_data): assert entry is not None +async def test_hardcoded_camera(hass, get_default_api_response): + """Test hardcoded camera""" + get_default_api_response["webcams"] = [] + + config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") + config_entry.add_to_hass(hass) + assert await async_setup_entry(hass, config_entry) + await hass.async_block_till_done() + + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("camera.mainsail_webcam") + + assert entry is not None + + async def test_thumbnail_camera_image( hass, aioclient_mock, get_data, _moonraker_default_mock ): From ae82afe9e65fe5612e870059f16f21db4c22375a Mon Sep 17 00:00:00 2001 From: Marc-Olivier Arsenault Date: Sun, 26 Nov 2023 17:43:51 -0500 Subject: [PATCH 3/4] add api do not return webcam list test --- tests/test_camera.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/test_camera.py b/tests/test_camera.py index 8e3df85..0c08fa8 100755 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -114,7 +114,7 @@ async def test_setup_thumbnail_camera(hass, get_data): assert entry is not None -async def test_hardcoded_camera(hass, get_default_api_response): +async def test_hardcoded_camera_empty_list(hass, get_default_api_response): """Test hardcoded camera""" get_default_api_response["webcams"] = [] @@ -129,6 +129,21 @@ async def test_hardcoded_camera(hass, get_default_api_response): assert entry is not None +async def test_hardcoded_camera_API_error(hass, get_default_api_response): + """Test hardcoded camera""" + get_default_api_response["webcams"] = None + + config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test") + config_entry.add_to_hass(hass) + assert await async_setup_entry(hass, config_entry) + await hass.async_block_till_done() + + entity_registry = er.async_get(hass) + entry = entity_registry.async_get("camera.mainsail_webcam") + + assert entry is not None + + async def test_thumbnail_camera_image( hass, aioclient_mock, get_data, _moonraker_default_mock ): From c78d488083427b0cb731d1e5873e45db83c153f7 Mon Sep 17 00:00:00 2001 From: Marc-Olivier Arsenault Date: Sun, 26 Nov 2023 17:45:38 -0500 Subject: [PATCH 4/4] remove unused try catch --- custom_components/moonraker/camera.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/custom_components/moonraker/camera.py b/custom_components/moonraker/camera.py index 19bd58c..80878f6 100755 --- a/custom_components/moonraker/camera.py +++ b/custom_components/moonraker/camera.py @@ -50,13 +50,10 @@ async def async_setup_entry( _LOGGER.info("Could not add any cameras from the API list") if camera_cnt == 0: - try: - _LOGGER.info("No Camera in the list, trying hardcoded") - async_add_entities( - [MoonrakerCamera(config_entry, coordinator, hardcoded_camera, 0)] - ) - except Exception: - _LOGGER.info("Could not add hardcoded camera") + _LOGGER.info("No Camera in the list, trying hardcoded") + async_add_entities( + [MoonrakerCamera(config_entry, coordinator, hardcoded_camera, 0)] + ) async_add_entities( [