Skip to content

Commit

Permalink
Merge pull request #1 from matmair/working-0.2.0
Browse files Browse the repository at this point in the history
Add notifications
  • Loading branch information
matmair authored Jun 25, 2023
2 parents 17bf719 + e55fc1e commit 9bebc32
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
62 changes: 34 additions & 28 deletions src/inventree_wled_locator/WledPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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),
],
},
}
13 changes: 0 additions & 13 deletions workflows/action.yaml

This file was deleted.

0 comments on commit 9bebc32

Please sign in to comment.