Skip to content

Commit

Permalink
add optional port for camera and thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolivierarsenault committed Aug 12, 2024
1 parent ef4a9fa commit 021765b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
26 changes: 23 additions & 3 deletions custom_components/moonraker/camera.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Support for Moonraker camera."""

from __future__ import annotations

import logging
Expand All @@ -15,12 +16,15 @@
CONF_URL,
CONF_OPTION_CAMERA_STREAM,
CONF_OPTION_CAMERA_SNAPSHOT,
CONF_OPTION_CAMERA_PORT,
CONF_OPTION_THUMBNAIL_PORT,
DOMAIN,
METHODS,
PRINTSTATES,
)

_LOGGER = logging.getLogger(__name__)
DEFAULT_PORT = 80

hardcoded_camera = {
"name": "webcam",
Expand Down Expand Up @@ -97,10 +101,18 @@ def __init__(self, config_entry, coordinator, camera, camera_id) -> None:
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, config_entry.entry_id)}
)
if (
config_entry.options.get(CONF_OPTION_CAMERA_PORT) is not None
and config_entry.options.get(CONF_OPTION_CAMERA_PORT) != ""
):
self.port = config_entry.options.get(CONF_OPTION_CAMERA_PORT)

Check warning on line 108 in custom_components/moonraker/camera.py

View check run for this annotation

Codecov / codecov/patch

custom_components/moonraker/camera.py#L108

Added line #L108 was not covered by tests
else:
self.port = DEFAULT_PORT

if camera["stream_url"].startswith("http"):
self.url = ""
else:
self.url = f"http://{config_entry.data.get(CONF_URL)}"
self.url = f"http://{config_entry.data.get(CONF_URL)}:{self.port}"

_LOGGER.info(f"Connecting to camera: {self.url}{camera['stream_url']}")

Expand Down Expand Up @@ -133,6 +145,14 @@ def __init__(self, config_entry, coordinator, session) -> None:
self._current_pic = None
self._current_path = ""

if (
config_entry.options.get(CONF_OPTION_THUMBNAIL_PORT) is not None
and config_entry.options.get(CONF_OPTION_THUMBNAIL_PORT) != ""
):
self.port = config_entry.options.get(CONF_OPTION_THUMBNAIL_PORT)

Check warning on line 152 in custom_components/moonraker/camera.py

View check run for this annotation

Codecov / codecov/patch

custom_components/moonraker/camera.py#L152

Added line #L152 was not covered by tests
else:
self.port = DEFAULT_PORT

async def async_camera_image(
self, width: int | None = None, height: int | None = None
) -> bytes | None:
Expand Down Expand Up @@ -163,10 +183,10 @@ async def async_camera_image(
new_path = new_path.replace(" ", "%20")

_LOGGER.debug(
f"Fetching new thumbnail: http://{self.url}/server/files/gcodes/{new_path}"
f"Fetching new thumbnail: http://{self.url}:{self.port}/server/files/gcodes/{new_path}"
)
response = await self._session.get(
f"http://{self.url}/server/files/gcodes/{new_path}"
f"http://{self.url}:{self.port}/server/files/gcodes/{new_path}"
)

self._current_path = new_path
Expand Down
14 changes: 14 additions & 0 deletions custom_components/moonraker/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
CONF_OPTION_POLLING_RATE,
CONF_OPTION_CAMERA_STREAM,
CONF_OPTION_CAMERA_SNAPSHOT,
CONF_OPTION_CAMERA_PORT,
CONF_OPTION_THUMBNAIL_PORT,
DOMAIN,
TIMEOUT,
)
Expand Down Expand Up @@ -187,6 +189,18 @@ async def async_step_init(
CONF_OPTION_CAMERA_SNAPSHOT, ""
),
): str,
vol.Optional(
CONF_OPTION_CAMERA_PORT,
default=self.config_entry.options.get(
CONF_OPTION_CAMERA_PORT, ""
),
): str,
vol.Optional(
CONF_OPTION_THUMBNAIL_PORT,
default=self.config_entry.options.get(
CONF_OPTION_THUMBNAIL_PORT, ""
),
): str,
}
),
)
2 changes: 2 additions & 0 deletions custom_components/moonraker/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
CONF_OPTION_CAMERA_STREAM = "camera_stream_url"
CONF_OPTION_CAMERA_SNAPSHOT = "camera_snapshot_url"
CONF_OPTION_POLLING_RATE = "polling_rate"
CONF_OPTION_CAMERA_PORT = "camera_port"
CONF_OPTION_THUMBNAIL_PORT = "thumbnail_port"

# API dict keys
HOSTNAME = "hostname"
Expand Down
4 changes: 3 additions & 1 deletion custom_components/moonraker/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"data": {
"polling_rate": "Integration polling rate (s)",
"camera_stream_url": "Camera Stream URL",
"camera_snapshot_url": "Camera Snapshot URL"
"camera_snapshot_url": "Camera Snapshot URL",
"camera_port": "Camera Port",
"thumbnail_port": "Thumbnail Port"
},
"title": "Configuration"
}
Expand Down
4 changes: 3 additions & 1 deletion tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ async def test_camera_services(hass, caplog):
entry = entity_registry.async_get("camera.mainsail_webcam")

assert entry is not None
assert "Connecting to camera: http://1.2.3.4/webcam/?action=stream" in caplog.text
assert (
"Connecting to camera: http://1.2.3.4:80/webcam/?action=stream" in caplog.text
)


async def test_camera_services_full_path(hass, get_camera_info, caplog):
Expand Down

0 comments on commit 021765b

Please sign in to comment.