Skip to content

Commit

Permalink
FIXED:
Browse files Browse the repository at this point in the history
- No device name on tooltip on level change
- Text file not written on level change
  • Loading branch information
Brian Russell committed Oct 26, 2023
1 parent bbe179f commit 0281ab7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
13 changes: 12 additions & 1 deletion device.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import asyncio
import websockets
import json
import logging

import websockets

from globals import Shared
from icons import get_icon


def get_device(id):
for dev in Shared.devices:
if dev.id == id:
return dev

class Device:
def __init__(self, id, unitId, name, batteryLevel, charging):
self.id = id
Expand Down Expand Up @@ -49,6 +58,8 @@ async def get_battery(self):
break

def select(self, tray):
logger = logging.getLogger(Shared.appname)
logger.info(f'Selected {self.name}')
Shared.selected_device = self.id
asyncio.run(self.get_battery())
if self.charging:
Expand Down
34 changes: 23 additions & 11 deletions lgbattery.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from logging.config import dictConfig
from infi.systray import SysTrayIcon

from device import Device
from device import Device, get_device
from icons import get_icon, export_icons
from globals import Shared

Expand Down Expand Up @@ -101,17 +101,29 @@ async def watch_battery():
if websocket.closed == False and response != None:
message = json.loads(response[0])
logger.debug(f"Received : {message}")

if message['path'] == '/battery/state/changed' and message['payload']['deviceId'] == Shared.selected_device:
charging = ''
if message['payload']['charging'] == True:
charging = ' (charging)'
level = message['payload']['percentage']
tooltip = f"{str(level)}%{charging}"
icon = get_icon(level)
logger.debug(f"Level={level}, Charging={
message['payload']['charging']}, Icon={icon}")

if message['path'] != '/battery/state/changed':
continue

device = get_device(message['payload']['deviceId'])
if None != device and device.id == Shared.selected_device:

device.charging = message['payload']['charging']
device.batteryLevel = message['payload']['percentage']

if device.charging:
tooltip = f'{device.name}: {str(device.batteryLevel)}% (charging)'
else:
tooltip = f'{device.name}: {str(device.batteryLevel)}%'
icon = get_icon(device.batteryLevel)

logger.info(f"Level={device.batteryLevel}, Charging={device.charging}, Icon={icon}")
Shared.systray.update(hover_text=tooltip, icon=icon)

if Shared.level_file != None:
with open(Shared.level_file, 'w') as f:
f.write(str(device.batteryLevel))

except asyncio.CancelledError:
logger.debug("await cancelled")
done = True
Expand Down

0 comments on commit 0281ab7

Please sign in to comment.