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"} diff --git a/src/inventree_wled_locator/WledPlugin.py b/src/inventree_wled_locator/WledPlugin.py index 59efaaa..3c64a96 100644 --- a/src/inventree_wled_locator/WledPlugin.py +++ b/src/inventree_wled_locator/WledPlugin.py @@ -3,8 +3,11 @@ 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 @@ -19,23 +22,39 @@ class WledPlugin(LocateMixin, SettingsMixin, InvenTreePlugin): SLUG = 'inventree-wled-locator' TITLE = "WLED Locator" - def set_led(self, target_led: int): + 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 = None): """Turn on a specific LED.""" base_url = f'http://{self.get_setting("ADDRESS")}/json/state' color_black = '000000' 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) + if target_led: + requests.post(base_url, json={"seg": {"i": [target_led, color_marked]}}) def locate_stock_location(self, location_pk): """Locate a StockLocation. @@ -47,26 +66,13 @@ 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(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_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!") - - 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), - ], - }, - } 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