Skip to content

Commit

Permalink
add hardcoded webcam (#231)
Browse files Browse the repository at this point in the history
* add hardcoded webcam

* add default test

* add api do not return webcam list test

* remove unused try catch
  • Loading branch information
marcolivierarsenault authored Nov 27, 2023
1 parent 925be80 commit 1277d04
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
30 changes: 27 additions & 3 deletions custom_components/moonraker/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down

0 comments on commit 1277d04

Please sign in to comment.