Skip to content

Commit

Permalink
z2m: update webif
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Dec 12, 2023
1 parent a91053d commit b38ced2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 54 deletions.
37 changes: 5 additions & 32 deletions zigbee2mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from lib.model.mqttplugin import MqttPlugin

from .rgbxy import Converter
# from .webif import WebInterface
from .webif import WebInterface

Z2M_TOPIC = 'z2m_topic'
Z2M_ATTR = 'z2m_attr'
Expand Down Expand Up @@ -62,6 +62,7 @@ def __init__(self, sh, **kwargs):
self.cycle = self.get_parameter_value('poll_period')
self.read_at_init = self.get_parameter_value('read_at_init')
self.bool_values = self.get_parameter_value('bool_values')
self._z2m_gui = self.get_parameter_value('z2m_gui')

self._items_read = []
self._items_write = []
Expand Down Expand Up @@ -102,6 +103,9 @@ def __init__(self, sh, **kwargs):
# Add subscription to get device announces
self.add_z2m_subscription('+', '', '', '', 'dict', callback=self.on_mqtt_msg)

# try to load webif
self.init_webinterface(WebInterface)

def run(self):
""" Run method for the plugin """

Expand Down Expand Up @@ -383,37 +387,6 @@ def on_mqtt_msg(self, topic: str, payload, qos=None, retain=None):
except Exception as e:
self.logger.debug(f"Error {e} occurred during decoding of last_seen using format '%Y-%m-%dT%H:%M:%SZ'.")

# # Korrektur der Brightness von 0-254 auf 0-100%
# if 'brightness' in payload:
# try:
# payload.update({'brightness': int(round(payload['brightness'] * 100 / 254, 0))})
# except Exception as e:
# self.logger.debug(f"Error {e} occurred during decoding of brightness.")

# # Korrektur der Farbtemperatur von "mired scale" (Reziproke Megakelvin) auf Kelvin
# if 'color_temp' in payload:
# try:
# # keep mired and "true" kelvin
# payload.update({'color_temp_mired': payload['color_temp']})
# payload.update({'color_temp_k': int(round(1000000 / int(payload['color_temp']), 0))})
#
# payload.update({'color_temp': int(round(10000 / int(payload['color_temp']), 0)) * 100})
# except Exception as e:
# self.logger.debug(f"Error {e} occurred during decoding of color_temp.")

# # Verarbeitung von Farbdaten
# if 'color_mode' in payload and 'color' in payload:
# color_mode = payload['color_mode']
# color = payload.pop('color')
#
# if color_mode == 'hs':
# payload['hue'] = color['hue']
# payload['saturation'] = color['saturation']
#
# if color_mode == 'xy':
# payload['color_x'] = color['x']
# payload['color_y'] = color['y']

if 'data' not in self._devices[device]:
self._devices[device]['data'] = {}
self._devices[device]['data'].update(payload)
Expand Down
26 changes: 13 additions & 13 deletions zigbee2mqtt/webif/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import cherrypy
from lib.item import Items
from lib.model.smartplugin import SmartPluginWebIf
from jinja2 import Environment, FileSystemLoader


class WebInterface(SmartPluginWebIf):
Expand Down Expand Up @@ -72,8 +71,8 @@ def index(self, reload=None, action=None):
return tmpl.render(plugin_shortname=self.plugin.get_shortname(),
plugin_version=self.plugin.get_version(),
plugin_info=self.plugin.get_info(),
items=sorted(self.plugin.zigbee2mqtt_items, key=lambda k: str.lower(k['_path'])),
item_count=len(self.plugin.zigbee2mqtt_items),
items=sorted([i for i in self.plugin.get_item_list()], key=lambda x: x.path().lower()),
item_count=len(self.plugin.get_item_list()),
p=self.plugin,
webif_pagelength=pagelength,
)
Expand All @@ -96,23 +95,24 @@ def get_data_html(self, dataSet=None):
data['broker_uptime'] = self.plugin.broker_uptime()

data['item_values'] = {}
for item in self.plugin.zigbee2mqtt_items:
for item in self.plugin.get_item_list():
data['item_values'][item.id()] = {}
data['item_values'][item.id()]['value'] = item.property.value
data['item_values'][item.id()]['last_update'] = item.property.last_update.strftime('%d.%m.%Y %H:%M:%S')
data['item_values'][item.id()]['last_change'] = item.property.last_change.strftime('%d.%m.%Y %H:%M:%S')

data['device_values'] = {}
for device in self.plugin.zigbee2mqtt_devices:
data['device_values'][device] = {}
if 'data' in self.plugin.zigbee2mqtt_devices[device]:
data['device_values'][device]['lqi'] = str(self.plugin.zigbee2mqtt_devices[device]['data'].get('linkquality', '-'))
data['device_values'][device]['data'] = ", ".join(list(self.plugin.zigbee2mqtt_devices[device]['data'].keys()))
for device in self.plugin._devices:
if 'data' in self.plugin._devices[device]:
data['device_values'][device] = {
'lqi': str(self.plugin._devices[device]['data'].get('linkquality', '-')),
'data': ", ".join(list(self.plugin._devices[device]['data'].keys()))
}
else:
data['device_values'][device]['lqi'] = '-'
data['device_values'][device]['data'] = '-'
if 'meta' in self.plugin.zigbee2mqtt_devices[device]:
last_seen = self.plugin.zigbee2mqtt_devices[device]['meta'].get('lastSeen', None)
data['device_values'][device] = {'lqi': '-', 'data': '-'}

if 'meta' in self.plugin._devices[device]:
last_seen = self.plugin._devices[device]['meta'].get('lastSeen', None)
if last_seen:
data['device_values'][device]['last_seen'] = last_seen.strftime('%d.%m.%Y %H:%M:%S')
else:
Expand Down
18 changes: 9 additions & 9 deletions zigbee2mqtt/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
<td class="py-1">{{ p.broker_config.qos }}</td>
<td class="py-1">{{ webif_pagelength }}</td>
<td class="py-1"><strong>{{_('Zigbee2Mqtt')}}</strong></td>
<td class="py-1"><a class="text-shng" href="http://{{ p.local_ip }}:8082" target="_blank">{{ 'GUI' }}</a></td>
<td class="py-1"><a class="text-shng" href="http://{{ p._z2m_gui }}" target="_blank">{{ 'GUI' }}</a></td>
<td class="py-1"></td>
</tr>
</tbody>
Expand Down Expand Up @@ -205,7 +205,7 @@
{% for item in items %}
{% set item_id = item.id() %}
<tr>
<td class="py-1">{{ item_id }}</td>
<td class="py-1">{{ item.property.path }}</td>
<td class="py-1">{{ item.property.type }}</td>
<td class="py-1" id="{{ item_id }}_value" style="text-align: right">{{ item()}}</td>
<td class="py-1">{{ p.get_iattr_value(item.conf, 'zigbee2mqtt_topic') }}</td>
Expand Down Expand Up @@ -238,16 +238,16 @@
</tr>
</thead>
<tbody>
{% for device in p.zigbee2mqtt_devices %}
{% if p.zigbee2mqtt_devices[device]['meta'] %}
{% for device in p._devices %}
{% if p._devices[device]['meta'] %}
<tr>
<td class="py-1">{{ loop.index }}</td>
<td class="py-1">{{ device }}</td>
<td class="py-1">{{ p.zigbee2mqtt_devices[device]['meta']['ieeeAddr'] }}</td>
<td class="py-1">{{ p.zigbee2mqtt_devices[device]['meta']['vendor'] }}</td>
<td class="py-1">{{ p.zigbee2mqtt_devices[device]['meta']['model'] }}</td>
<td class="py-1">{{ p.zigbee2mqtt_devices[device]['meta']['description'] }}</td>
<td class="py-1">{{ p.zigbee2mqtt_devices[device]['meta']['modelID'] }}</td>
<td class="py-1">{{ p._devices[device]['meta']['ieee_address'] }}</td>
<td class="py-1">{{ p._devices[device]['meta']['manufacturer'] }}</td>
<td class="py-1">{{ p._devices[device]['meta']['model'] }}</td>
<td class="py-1">{{ p._devices[device]['meta']['description'] }}</td>
<td class="py-1">{{ p._devices[device]['meta']['model_id'] }}</td>
<td class="py-1" id="{{ device }}_last_seen">{{ _('Init...') }}</td>
<td class="py-1" id="{{ device }}_lqi">{{ _('Init...') }}</td>
<td class="py-1" id="{{ device }}_data">{{ _('Init...') }}</td>
Expand Down

0 comments on commit b38ced2

Please sign in to comment.