|
11 | 11 | from logging.config import dictConfig
|
12 | 12 | from infi.systray import SysTrayIcon
|
13 | 13 |
|
14 |
| -from device import Device |
| 14 | +from device import Device, get_device |
15 | 15 | from icons import get_icon, export_icons
|
16 | 16 | from globals import Shared
|
17 | 17 |
|
@@ -101,17 +101,29 @@ async def watch_battery():
|
101 | 101 | if websocket.closed == False and response != None:
|
102 | 102 | message = json.loads(response[0])
|
103 | 103 | 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}") |
114 | 121 | 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 | + |
115 | 127 | except asyncio.CancelledError:
|
116 | 128 | logger.debug("await cancelled")
|
117 | 129 | done = True
|
|
0 commit comments