Skip to content

Commit

Permalink
Add scale and status options to take snapshot and add snapshot to gif…
Browse files Browse the repository at this point in the history
… services
  • Loading branch information
godely committed Jun 28, 2022
1 parent 81213a2 commit 2452cca
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
2 changes: 2 additions & 0 deletions custom_components/dremel_3d_printer/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@
ATTR_FILENAME = "filename"
ATTR_FPS = "fps"
ATTR_DURATION = "duration"
ATTR_SHOW_STATUS = "show_status"
ATTR_SCALE = "scale"

EVENT_DATA_NEW_PRINT_STATS = "dremel_3d_printer_new_print_stats"
4 changes: 2 additions & 2 deletions custom_components/dremel_3d_printer/manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"domain": "dremel_3d_printer",
"version": "1.1.4",
"version": "1.2.0",
"name": "Dremel 3D Printer",
"config_flow": true,
"documentation": "https://github.com/godely/dremel-3d-printer-homeassistant",
"issue_tracker": "https://github.com/godely/dremel-3d-printer-homeassistant/issues",
"requirements": ["dremel3dpy==2.0.1"],
"requirements": ["dremel3dpy==2.1.1"],
"codeowners": ["@godely"],
"iot_class": "local_polling"
}
28 changes: 17 additions & 11 deletions custom_components/dremel_3d_printer/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
ATTR_FPS,
ATTR_NAME,
ATTR_OUTPUT_DIR,
ATTR_SCALE,
ATTR_SHOW_STATUS,
ATTR_URL,
DOMAIN,
EVENT_DATA_NEW_PRINT_STATS,
Expand Down Expand Up @@ -52,13 +54,17 @@
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Required(ATTR_OUTPUT_DIR): cv.string,
vol.Optional(ATTR_FILENAME): cv.string,
vol.Optional(ATTR_SHOW_STATUS): cv.boolean,
vol.Optional(ATTR_SCALE): cv.small_float,
}
)

SERVICE_ADD_TO_GIF_SCHEMA = vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.string,
vol.Optional(ATTR_NAME): cv.string,
vol.Optional(ATTR_SHOW_STATUS): cv.boolean,
vol.Optional(ATTR_SCALE): cv.small_float,
}
)

Expand Down Expand Up @@ -143,12 +149,14 @@ async def take_snapshot(service: ServiceCall) -> None:
"""Service to take a snapshot and add it to the gif with the given name."""
api = get_api(service)
camera = Dremel3D45Timelapse(api, None)
show_status = service.data.get(ATTR_SHOW_STATUS) or False
scale = service.data.get(ATTR_SCALE) or 1.0
print(not show_status)
snapshot = await hass.async_add_executor_job(
camera.get_snapshot_as_ndarray
camera.get_snapshot_as_ndarray, not show_status, scale
)
output_dir = service.data.get(ATTR_OUTPUT_DIR)
if (name := service.data.get(ATTR_FILENAME)) is None:
name = str(datetime.datetime.now())
name = service.data.get(ATTR_FILENAME) or str(datetime.datetime.now())
await hass.async_add_executor_job(
write_snapshot, hass, output_dir, name, snapshot
)
Expand All @@ -157,12 +165,12 @@ async def add_snapshot_to_gif(service: ServiceCall) -> None:
"""Service to take a snapshot and add it to the gif with the given name."""
api = get_api(service)
camera = Dremel3D45Timelapse(api, None)
show_status = service.data.get(ATTR_SHOW_STATUS) or False
scale = service.data.get(ATTR_SCALE) or 1.0
snapshot = await hass.async_add_executor_job(
camera.get_snapshot_as_ndarray
camera.get_snapshot_as_ndarray, not show_status, scale
)
name = service.data.get(ATTR_NAME)
if name is None:
name = api.get_job_name()
name = service.data.get(ATTR_NAME) or api.get_job_name()
gifmaker = GifMaker(hass, name)
await hass.async_add_executor_job(
gifmaker.add_snapshot, snapshot
Expand All @@ -171,14 +179,12 @@ async def add_snapshot_to_gif(service: ServiceCall) -> None:
async def make_gif(service: ServiceCall) -> None:
"""Service to render the gif with the given name."""
api = get_api(service)
name = service.data.get(ATTR_NAME)
if name is None:
name = api.get_job_name()
name = service.data.get(ATTR_NAME) or api.get_job_name()
output_dir = service.data.get(ATTR_OUTPUT_DIR)
fps = service.data.get(ATTR_FPS)
duration = service.data.get(ATTR_DURATION)
if fps is not None and duration is not None:
raise Exception("You should specify exactly one of FPS or Duration.")
fps = None
elif fps is None and duration is None:
fps = 10
if fps:
Expand Down
43 changes: 39 additions & 4 deletions custom_components/dremel_3d_printer/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ stop_job:
integration: dremel_3d_printer
manufacturer: Dremel
take_snapshot:
name: Take a Snapshot
name: Take a snapshot
description: Take a snapshot using the current camera.
fields:
device_id:
Expand All @@ -84,8 +84,25 @@ take_snapshot:
example: "foobar"
selector:
text:
show_status:
name: Show status
description: Add status information to the snapshot, such as a progress bar.
example: False
default: false
selector:
boolean:
scale:
name: Snapshot scale
description: Scale of the snapshot, in which 1.0 yields a 640x480 image. Default to 1.0.
example: 1.0
default: 1.0
selector:
number:
min: 0.0
max: 1.0
step: 0.01
add_snapshot_to_gif:
name: Add Snapshot to Gif
name: Add snapshot to gif
description: Takes a snapshot with the printer camera and adds the image to the gif building process.
fields:
device_id:
Expand All @@ -103,6 +120,23 @@ add_snapshot_to_gif:
example: "mygif"
selector:
text:
show_status:
name: Show status
description: Add status information to the snapshot, such as a progress bar. Default to false.
example: False
default: false
selector:
boolean:
scale:
name: Snapshot scale
description: Scale of the snapshot, in which 1.0 yields a 640x480 image. Default to 1.0.
example: 1.0
default: 1.0
selector:
number:
min: 0.0
max: 1.0
step: 0.01
make_gif:
name: Make Gif
description: Renders the gif from the images added to the gif building process with the given name.
Expand Down Expand Up @@ -131,13 +165,14 @@ make_gif:
text:
fps:
name: FPS
description: FPS of the gif file. Either FPS or Duration must be defined, but not both.
description: FPS of the gif file.
example: 10
default: 10
selector:
text:
duration:
name: Duration
description: Duration of each frame in the gif file. Either FPS or Duration must be defined, but not both.
description: Duration of each frame in the gif file. If set, will override FPS value.
example: 0.1
selector:
text:

0 comments on commit 2452cca

Please sign in to comment.