From 2452cca89822dc30ec990de8b1c37625255d57b6 Mon Sep 17 00:00:00 2001 From: Gustavo Stor Date: Tue, 28 Jun 2022 14:55:10 -0400 Subject: [PATCH] Add scale and status options to take snapshot and add snapshot to gif services --- custom_components/dremel_3d_printer/const.py | 2 + .../dremel_3d_printer/manifest.json | 4 +- .../dremel_3d_printer/services.py | 28 +++++++----- .../dremel_3d_printer/services.yaml | 43 +++++++++++++++++-- 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/custom_components/dremel_3d_printer/const.py b/custom_components/dremel_3d_printer/const.py index e87899e..7f3266a 100644 --- a/custom_components/dremel_3d_printer/const.py +++ b/custom_components/dremel_3d_printer/const.py @@ -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" diff --git a/custom_components/dremel_3d_printer/manifest.json b/custom_components/dremel_3d_printer/manifest.json index 81fa25a..ab8bcf1 100644 --- a/custom_components/dremel_3d_printer/manifest.json +++ b/custom_components/dremel_3d_printer/manifest.json @@ -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" } \ No newline at end of file diff --git a/custom_components/dremel_3d_printer/services.py b/custom_components/dremel_3d_printer/services.py index 90ca40d..e4b999e 100644 --- a/custom_components/dremel_3d_printer/services.py +++ b/custom_components/dremel_3d_printer/services.py @@ -21,6 +21,8 @@ ATTR_FPS, ATTR_NAME, ATTR_OUTPUT_DIR, + ATTR_SCALE, + ATTR_SHOW_STATUS, ATTR_URL, DOMAIN, EVENT_DATA_NEW_PRINT_STATS, @@ -52,6 +54,8 @@ 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, } ) @@ -59,6 +63,8 @@ { 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, } ) @@ -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 ) @@ -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 @@ -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: diff --git a/custom_components/dremel_3d_printer/services.yaml b/custom_components/dremel_3d_printer/services.yaml index 28b7c4d..32376e6 100644 --- a/custom_components/dremel_3d_printer/services.yaml +++ b/custom_components/dremel_3d_printer/services.yaml @@ -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: @@ -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: @@ -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. @@ -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: \ No newline at end of file