From 7554c98153ca9ee7afad8b4ff9698850f68b955e Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 25 Jun 2023 20:44:11 +0200 Subject: [PATCH 1/7] cleanup --- workflows/action.yaml | 13 ------------- 1 file changed, 13 deletions(-) delete mode 100644 workflows/action.yaml diff --git a/workflows/action.yaml b/workflows/action.yaml deleted file mode 100644 index ca426e5..0000000 --- a/workflows/action.yaml +++ /dev/null @@ -1,13 +0,0 @@ -name: Plugin action -on: - push: - branches: ['main', 'master'] - pull_request: - types: [opened, edited, reopened] - release: - types: [published] - -jobs: - plugin-action: - uses: matmair/inventree-meta-plugin/.github/workflows/plugin_action.yaml@main - secrets: inherit From 9bc3bb1d10f46ba978aaad81dd19bc62ff938088 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 25 Jun 2023 20:45:20 +0200 Subject: [PATCH 2/7] Add notifications --- src/inventree_wled_locator/WledPlugin.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/inventree_wled_locator/WledPlugin.py b/src/inventree_wled_locator/WledPlugin.py index 59efaaa..d5f195c 100644 --- a/src/inventree_wled_locator/WledPlugin.py +++ b/src/inventree_wled_locator/WledPlugin.py @@ -8,6 +8,8 @@ from plugin import InvenTreePlugin from plugin.mixins import LocateMixin, SettingsMixin from stock.models import StockLocation +from common.notifications import NotificationBody +from InvenTree.helpers_model import notify_responsible logger = logging.getLogger('inventree') @@ -47,14 +49,23 @@ def locate_stock_location(self, location_pk): try: location = StockLocation.objects.get(pk=location_pk) - logger.info(f"Location exists at '{location.pathstring}'") - - # Tag metadata - self.set_led(13, location.get_metadata('wled_led')) + led_nbr = location.get_metadata('wled_led') + if led_nbr: + self.set_led(13, led_nbr) + else: + # notify superusers that a location has no LED number + logger.error(f"Location ID {location_pk} has no WLED LED number!") + notify_responsible(location, StockLocation, content=self.NO_LED_NOTIFICATION) except (ValueError, StockLocation.DoesNotExist): # pragma: no cover logger.error(f"Location ID {location_pk} does not exist!") + NO_LED_NOTIFICATION = NotificationBody( + name=_("No location for {verbose_name}"), + slug='{app_label}.no_led_{model_name}', + message=_("No LED number is assigned for {verbose_name} {name}"), + ) + SETTINGS = { 'ADDRESS': { 'name': _('IP Address'), From 5e3e19368d142c8040eb22181864e8b932f3d226 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 25 Jun 2023 21:18:11 +0200 Subject: [PATCH 3/7] fix syntax --- src/inventree_wled_locator/WledPlugin.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/inventree_wled_locator/WledPlugin.py b/src/inventree_wled_locator/WledPlugin.py index d5f195c..25aec8b 100644 --- a/src/inventree_wled_locator/WledPlugin.py +++ b/src/inventree_wled_locator/WledPlugin.py @@ -3,13 +3,14 @@ import logging import requests +from common.notifications import NotificationBody +from django.contrib.auth import get_user_model from django.core.validators import MinValueValidator from django.utils.translation import ugettext_lazy as _ +from InvenTree.helpers_model import notify_users from plugin import InvenTreePlugin from plugin.mixins import LocateMixin, SettingsMixin from stock.models import StockLocation -from common.notifications import NotificationBody -from InvenTree.helpers_model import notify_responsible logger = logging.getLogger('inventree') @@ -21,6 +22,8 @@ class WledPlugin(LocateMixin, SettingsMixin, InvenTreePlugin): SLUG = 'inventree-wled-locator' TITLE = "WLED Locator" + superusers = list(get_user_model().objects.filter(is_superuser=True).all()) + def set_led(self, target_led: int): """Turn on a specific LED.""" base_url = f'http://{self.get_setting("ADDRESS")}/json/state' @@ -55,7 +58,7 @@ def locate_stock_location(self, location_pk): else: # notify superusers that a location has no LED number logger.error(f"Location ID {location_pk} has no WLED LED number!") - notify_responsible(location, StockLocation, content=self.NO_LED_NOTIFICATION) + notify_users(self.superusers, location, StockLocation, content=self.NO_LED_NOTIFICATION) except (ValueError, StockLocation.DoesNotExist): # pragma: no cover logger.error(f"Location ID {location_pk} does not exist!") @@ -63,7 +66,7 @@ def locate_stock_location(self, location_pk): NO_LED_NOTIFICATION = NotificationBody( name=_("No location for {verbose_name}"), slug='{app_label}.no_led_{model_name}', - message=_("No LED number is assigned for {verbose_name} {name}"), + message=_("No LED number is assigned for {verbose_name}"), ) SETTINGS = { @@ -75,9 +78,6 @@ def locate_stock_location(self, location_pk): 'name': _('Max LEDs'), 'description': _('Maximum number of LEDs in your WLED device'), 'default': 1, - 'validator': [ - int, - MinValueValidator(1), - ], + 'validator': [int, MinValueValidator(1), ], }, } From b2c265dd289caf12151836cb1194c91c463b5f62 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 25 Jun 2023 21:18:18 +0200 Subject: [PATCH 4/7] fix led call --- src/inventree_wled_locator/WledPlugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inventree_wled_locator/WledPlugin.py b/src/inventree_wled_locator/WledPlugin.py index 25aec8b..696822c 100644 --- a/src/inventree_wled_locator/WledPlugin.py +++ b/src/inventree_wled_locator/WledPlugin.py @@ -54,7 +54,7 @@ def locate_stock_location(self, location_pk): location = StockLocation.objects.get(pk=location_pk) led_nbr = location.get_metadata('wled_led') if led_nbr: - self.set_led(13, led_nbr) + self.set_led(led_nbr) else: # notify superusers that a location has no LED number logger.error(f"Location ID {location_pk} has no WLED LED number!") From 3ed0e96fd6f60cf9c021e7b26ad1198d3676e31c Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 25 Jun 2023 21:19:52 +0200 Subject: [PATCH 5/7] cleanup led function --- src/inventree_wled_locator/WledPlugin.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/inventree_wled_locator/WledPlugin.py b/src/inventree_wled_locator/WledPlugin.py index 696822c..85ad143 100644 --- a/src/inventree_wled_locator/WledPlugin.py +++ b/src/inventree_wled_locator/WledPlugin.py @@ -31,17 +31,11 @@ def set_led(self, target_led: int): color_marked = 'FF0000' # Turn off all segments - json = requests.get(base_url).json() - print(json) - requests.post(base_url, json={"seg": {"i": [0, self.get_setting("MAX_LEDS"), color_black]}}) # Turn on target led requests.post(base_url, json={"seg": {"i": [target_led, color_marked]}}) - json = requests.get(base_url).json() - print(json) - def locate_stock_location(self, location_pk): """Locate a StockLocation. From 35e2e9c8581cef2127a4f8dfd10f9f46c2c1665b Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 25 Jun 2023 21:46:36 +0200 Subject: [PATCH 6/7] move definition --- src/inventree_wled_locator/WledPlugin.py | 43 ++++++++++++------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/inventree_wled_locator/WledPlugin.py b/src/inventree_wled_locator/WledPlugin.py index 85ad143..3c64a96 100644 --- a/src/inventree_wled_locator/WledPlugin.py +++ b/src/inventree_wled_locator/WledPlugin.py @@ -22,9 +22,28 @@ class WledPlugin(LocateMixin, SettingsMixin, InvenTreePlugin): SLUG = 'inventree-wled-locator' TITLE = "WLED Locator" + NO_LED_NOTIFICATION = NotificationBody( + name=_("No location for {verbose_name}"), + slug='{app_label}.no_led_{model_name}', + message=_("No LED number is assigned for {verbose_name}"), + ) + + SETTINGS = { + 'ADDRESS': { + 'name': _('IP Address'), + 'description': _('IP address of your WLED device'), + }, + 'MAX_LEDS': { + 'name': _('Max LEDs'), + 'description': _('Maximum number of LEDs in your WLED device'), + 'default': 1, + 'validator': [int, MinValueValidator(1), ], + }, + } + superusers = list(get_user_model().objects.filter(is_superuser=True).all()) - def set_led(self, target_led: int): + def set_led(self, target_led: int = None): """Turn on a specific LED.""" base_url = f'http://{self.get_setting("ADDRESS")}/json/state' color_black = '000000' @@ -34,7 +53,8 @@ def set_led(self, target_led: int): requests.post(base_url, json={"seg": {"i": [0, self.get_setting("MAX_LEDS"), color_black]}}) # Turn on target led - requests.post(base_url, json={"seg": {"i": [target_led, color_marked]}}) + if target_led: + requests.post(base_url, json={"seg": {"i": [target_led, color_marked]}}) def locate_stock_location(self, location_pk): """Locate a StockLocation. @@ -56,22 +76,3 @@ def locate_stock_location(self, location_pk): except (ValueError, StockLocation.DoesNotExist): # pragma: no cover logger.error(f"Location ID {location_pk} does not exist!") - - NO_LED_NOTIFICATION = NotificationBody( - name=_("No location for {verbose_name}"), - slug='{app_label}.no_led_{model_name}', - message=_("No LED number is assigned for {verbose_name}"), - ) - - SETTINGS = { - 'ADDRESS': { - 'name': _('IP Address'), - 'description': _('IP address of your WLED device'), - }, - 'MAX_LEDS': { - 'name': _('Max LEDs'), - 'description': _('Maximum number of LEDs in your WLED device'), - 'default': 1, - 'validator': [int, MinValueValidator(1), ], - }, - } From e55fc1e7686874015314c22b764d4e4e3c967e5c Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 25 Jun 2023 21:47:22 +0200 Subject: [PATCH 7/7] bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 3037547..0de9f26 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta" [project] name = "inventree-wled-locator" -version = "0.1.0" +version = "0.2.0" description="Use WLED to locate InvenTree StockLocations." readme = "README.md" license = {text = "MIT license"}