diff --git a/custom_components/moonraker/camera.py b/custom_components/moonraker/camera.py index 8f8e48a..80878f6 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,11 +37,22 @@ 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 + + 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") - for camera_id, camera in enumerate(cameras["webcams"]): + if camera_cnt == 0: + _LOGGER.info("No Camera in the list, trying hardcoded") async_add_entities( - [MoonrakerCamera(config_entry, coordinator, camera, camera_id)] + [MoonrakerCamera(config_entry, coordinator, hardcoded_camera, 0)] ) async_add_entities( diff --git a/tests/test_camera.py b/tests/test_camera.py index 2b68089..0c08fa8 100755 --- a/tests/test_camera.py +++ b/tests/test_camera.py @@ -114,6 +114,36 @@ async def test_setup_thumbnail_camera(hass, get_data): assert entry is not None +async def test_hardcoded_camera_empty_list(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_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 ):