Repository: pwr-Solaar/Solaar

Issue:
MK270 keyboard shows Battery status 30%  even with full batteries
**Information**
<!-- Make sure that your issue is not one of the known issues in the Solaar documentation at https://pwr-solaar.github.io/Solaar/ -->
<!-- Do not bother opening an issue for a version older than 1.1.0.  Update to the latest version and see if your issue persists. -->
- Solaar version (`solaar --version` or `git describe --tags` if cloned from this repository): `1.1.1-10-gf7a0422`
- Distribution: `ubuntu 18.04LTS`
- Kernel version (ex. `uname -srmo`): `Linux 5.4.0-91-generic x86_64 GNU/Linux`
- Output of `solaar show`:

<details>

```
 Solaar version 1.1.1

Nano Receiver
  Device path  : /dev/hidraw1
  USB id       : 046d:C534
  Serial       : None
    Firmware   : 29.01.B0016
  Has 2 paired device(s) out of a maximum of 2.
  Has 17 successful pairing(s) remaining.
  Notifications: wireless, software present (0x000900)

  1: Wireless Keyboard MK270
     Device path  : /dev/hidraw2
     WPID         : 4023
     Codename     : MK270
     Kind         : keyboard
     Protocol     : HID++ 2.0
     Polling rate : 20 ms (50Hz)
     Serial number: 3ED1C02F
     Model ID:      000000000000
     Unit ID:       00000000
          Firmware: RQK 49.00.B0029
     Supports 18 HID++ 2.0 features:
         0: ROOT                   {0000}   
         1: FEATURE SET            {0001}   
         2: DEVICE FW VERSION      {0003}   
            Firmware: Firmware RQK 49.00.B0029 4023
            Unit ID: 00000000  Model ID: 000000000000  Transport IDs: {}
         3: DEVICE NAME            {0005}   
            Name: Wireless Keyboard  
            Kind: keyboard
         4: BATTERY STATUS         {1000}   
            Battery: 30%, discharging, next level 5%.
         5: REPROG CONTROLS        {1B00}   
         6: WIRELESS DEVICE STATUS {1D4B}   
         7: FN INVERSION           {40A0}   
         8: ENCRYPTION             {4100}   
         9: KEYBOARD LAYOUT        {4520}   
        10: unknown:1810           {1810}   internal, hidden
        11: unknown:1830           {1830}   internal, hidden
        12: unknown:1890           {1890}   internal, hidden
        13: unknown:18A0           {18A0}   internal, hidden
        14: unknown:18B0           {18B0}   internal, hidden
        15: unknown:1DF3           {1DF3}   internal, hidden
        16: unknown:1E00           {1E00}   hidden
        17: unknown:1868           {1868}   internal, hidden
     Battery: 30%, discharging, next level 5%.

  2: Wireless Mouse M185 new
     Device path  : /dev/hidraw3
     WPID         : 4054
     Codename     : M185n
     Kind         : mouse
     Protocol     : HID++ 4.5
     Polling rate : 8 ms (125Hz)
     Serial number: D6266A9A
     Model ID:      000000000000
     Unit ID:       00000000
          Firmware: RQM 64.00.B0008
     Supports 20 HID++ 2.0 features:
         0: ROOT                   {0000}   
         1: FEATURE SET            {0001}   
         2: DEVICE FW VERSION      {0003}   
            Firmware: Firmware RQM 64.00.B0008 4054
            Unit ID: 00000000  Model ID: 000000000000  Transport IDs: {}
         3: DEVICE NAME            {0005}   
            Name: Wireless Mouse  
            Kind: mouse
         4: RESET                  {0020}   
         5: REPROG CONTROLS V4     {1B04}   
         6: WIRELESS DEVICE STATUS {1D4B}   
         7: LOWRES WHEEL           {2130}   
            Wheel Reports: HID
         8: POINTER SPEED          {2205}   
            Pointer Speed: 1.0
         9: DEVICE RESET           {1802}   internal, hidden
        10: unknown:1810           {1810}   internal, hidden
        11: unknown:1830           {1830}   internal, hidden
        12: unknown:1850           {1850}   internal, hidden
        13: unknown:1869           {1869}   internal, hidden
        14: unknown:1890           {1890}   internal, hidden
        15: unknown:18B1           {18B1}   internal, hidden
        16: unknown:1DF3           {1DF3}   internal, hidden
        17: unknown:1E00           {1E00}   hidden
        18: unknown:1F03           {1F03}   internal, hidden
        19: unknown:1E80           {1E80}   internal, hidden
     Has 3 reprogrammable keys:
         0: Left Button               , default: Left Click                  => Left Click                
             mse, reprogrammable, pos:0, group:1, group mask:g1
             reporting: default
         1: Right Button              , default: Right Click                 => Right Click               
             mse, reprogrammable, pos:0, group:1, group mask:g1
             reporting: default
         2: Middle Button             , default: Mouse Middle Button         => Mouse Middle Button       
             mse, reprogrammable, divertable, raw XY, pos:0, group:2, group mask:g1,g2
             reporting: default
     Battery status unavailable.
```
</details>

**Describe the bug**
New user here. I have the MK235 keyboard / mouse combo (K235 / M170). Based on the wireless PID they display as MK270 (4023) and M185n (4054). The original Nano receiver got lost and I ordered a cheap replacement which turned out to be a c534. 

I cloned the repo, copied the udev rules, used `solaar pair` to pair the devices with the new receiver, and both the keyboard and mouse work, which is great!! The keyboard was not working properly at first, turned out the connection was too weak, and had to bring it closer to the receiver.

However the battery status for the keyboard always shows 30%. At first i thought it was the batteries of the keyboard, as I took batteries I used previously for a flashlight. 30% battery left made sense. However, I just bought new high end batteries in the store, and it still shows 30% battery left. I also see others with the PID 4023 reporting 30% (e.g. https://github.com/pwr-Solaar/Solaar/issues/234#issuecomment-259575033 , and https://github.com/pwr-Solaar/Solaar/issues/1360 )

So I guess that the c534 receiver is not relaying battery status information properly? Or is the K235 / PID 4023 the culprit? That it always sends `BATTERY STATUS         {1000}` ? 
Searching the issues for `4023 STATUS 1000` returns several posts that also include the c534 (But of course the nano receiver often goes together with a MK270 keyboard). I found this old post https://github.com/pwr-Solaar/Solaar/issues/152#issuecomment-38780880 where it says:

`Since there are only two reportable levels, it is possible that the "30%" value means "near full" and "5%" means "near empty".`

This sounds plausible. If that is so, would it not be better simply to disable the display of battery status for the 4023 keyboard? Now it provides the wrong information, which confuses users.

(Thanks a lot for creating / maintaining this by the way)

**To Reproduce**
Steps to reproduce the behavior:
Run `solaar show` with a MK270 (K235) keyboard.

**Screenshots**


**Additional context**






Discussion:
On second thought, now that I know what it means, i would like to know when it starts reporting '5%' ... so disabling it might not be the best solution either. 
Solaar is reporting what the device is reporting.  Some devices report 90/50/5 (or something along these lines).  
A reasonable change to Solaar would be to show the good icon for 30% or over.  Currently the lower limit for good is 50%.

The quickest way to see how this works is for you to modify the code in your clone.    In lib/solaar/ui/icons.py look for the line
```
    level_name = _first_res(level, ((90, 'full'), (50, 'good'), (20, 'low'), (5, 'caution'), (0, 'empty')))
```
Change 50 to 30, Quit ouf of Solaar and run it again.  Report whether the battery icon has changed.
By the way, the receiver has nothing to do with the battery reporting.
Yes this works me! I had to install `gir1.2-ayatanaappindicator3-0.1`, after that the system tray icon became available.
Changing icons.py as suggested above indeed changes the icons (in the tray and in the gui) from "low" to "good" . 

Only thing left is to figure out how to automatically run solaar in the background after startup.
forgot to say thank you!!!
OK,  I'll make the change.
To start Solaar automatically depends on your distribution and windowing system.
Tnx! I created a file `~/.xsessionrc` with `~/Github/pwr-Solaar/Solaar/bin/solaar -w hide&` in it. This adds the system tray icon every time I log in.

Resolving patch:
diff --git a/lib/solaar/ui/icons.py b/lib/solaar/ui/icons.py
--- a/lib/solaar/ui/icons.py
+++ b/lib/solaar/ui/icons.py
@@ -127,7 +127,7 @@ def _battery_icon_name(level, charging):
     if level is None or level < 0:
         return 'battery-missing' + ('-symbolic' if gtk.battery_icons_style == 'symbolic' else '')
 
-    level_name = _first_res(level, ((90, 'full'), (50, 'good'), (20, 'low'), (5, 'caution'), (0, 'empty')))
+    level_name = _first_res(level, ((90, 'full'), (30, 'good'), (20, 'low'), (5, 'caution'), (0, 'empty')))
     return 'battery-%s%s%s' % (
         level_name, '-charging' if charging else '', '-symbolic' if gtk.battery_icons_style == 'symbolic' else ''
     )


Test patch: