Skip to content

Commit 0281ab7

Browse files
author
Brian Russell
committed
FIXED:
- No device name on tooltip on level change - Text file not written on level change
1 parent bbe179f commit 0281ab7

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

device.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import asyncio
2-
import websockets
32
import json
3+
import logging
4+
5+
import websockets
6+
47
from globals import Shared
58
from icons import get_icon
69

10+
11+
def get_device(id):
12+
for dev in Shared.devices:
13+
if dev.id == id:
14+
return dev
15+
716
class Device:
817
def __init__(self, id, unitId, name, batteryLevel, charging):
918
self.id = id
@@ -49,6 +58,8 @@ async def get_battery(self):
4958
break
5059

5160
def select(self, tray):
61+
logger = logging.getLogger(Shared.appname)
62+
logger.info(f'Selected {self.name}')
5263
Shared.selected_device = self.id
5364
asyncio.run(self.get_battery())
5465
if self.charging:

lgbattery.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from logging.config import dictConfig
1212
from infi.systray import SysTrayIcon
1313

14-
from device import Device
14+
from device import Device, get_device
1515
from icons import get_icon, export_icons
1616
from globals import Shared
1717

@@ -101,17 +101,29 @@ async def watch_battery():
101101
if websocket.closed == False and response != None:
102102
message = json.loads(response[0])
103103
logger.debug(f"Received : {message}")
104-
105-
if message['path'] == '/battery/state/changed' and message['payload']['deviceId'] == Shared.selected_device:
106-
charging = ''
107-
if message['payload']['charging'] == True:
108-
charging = ' (charging)'
109-
level = message['payload']['percentage']
110-
tooltip = f"{str(level)}%{charging}"
111-
icon = get_icon(level)
112-
logger.debug(f"Level={level}, Charging={
113-
message['payload']['charging']}, Icon={icon}")
104+
105+
if message['path'] != '/battery/state/changed':
106+
continue
107+
108+
device = get_device(message['payload']['deviceId'])
109+
if None != device and device.id == Shared.selected_device:
110+
111+
device.charging = message['payload']['charging']
112+
device.batteryLevel = message['payload']['percentage']
113+
114+
if device.charging:
115+
tooltip = f'{device.name}: {str(device.batteryLevel)}% (charging)'
116+
else:
117+
tooltip = f'{device.name}: {str(device.batteryLevel)}%'
118+
icon = get_icon(device.batteryLevel)
119+
120+
logger.info(f"Level={device.batteryLevel}, Charging={device.charging}, Icon={icon}")
114121
Shared.systray.update(hover_text=tooltip, icon=icon)
122+
123+
if Shared.level_file != None:
124+
with open(Shared.level_file, 'w') as f:
125+
f.write(str(device.batteryLevel))
126+
115127
except asyncio.CancelledError:
116128
logger.debug("await cancelled")
117129
done = True

0 commit comments

Comments
 (0)