Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hardcoded webcam #231

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading