Repository: pwr-Solaar/Solaar

Issue:
"Battery Good" = low battery icon ?
**Information**

- Output of `solaar show`:
```
sudo solaar show
[sudo] password for andre: 
Unifying Receiver
  Device path  : /dev/hidraw3
  USB id       : 046d:c52b
  Serial       : CD0A94D5
    Firmware   : 12.10.B0032
    Bootloader : 02.14
    Other      : AA.AA
  Has 3 paired device(s) out of a maximum of 6.
  Notifications: wireless (0x000100)
  Device activity counters: 1=131, 2=96

  1: Performance Mouse MX
     Codename     : Performance MX
     Kind         : mouse
     Wireless PID : 101A
     Protocol     : HID++ 1.0
     Polling rate : 8 ms (125Hz)
     Serial number: 01396843
          Firmware: 15.01.B0062
        Bootloader: 02.11
             Other: 00.09
     The power switch is located on the base.
     Notifications: battery status (0x100000).
     Battery: full, discharging.

  2: Wireless Solar Keyboard K750
     Codename     : K750
     Kind         : keyboard
     Wireless PID : 4002
     Protocol     : HID++ 2.0
     Polling rate : 20 ms (50Hz)
     Serial number: 72D5DF87
          Firmware: RQK 33.00.B0015
        Bootloader: DFU 00.02.B0003
     The power switch is located on the edge of top right corner.
     Supports 11 HID++ 2.0 features:
         0: ROOT                   {0000}   
         1: FEATURE SET            {0001}   
         2: DEVICE FW VERSION      {0003}   
         3: DEVICE NAME            {0005}   
         4: REPROG CONTROLS        {1B00}   
         5: WIRELESS DEVICE STATUS {1D4B}   
         6: unknown:1DF3           {1DF3}   hidden
         7: FN INVERSION           {40A0}   
         8: ENCRYPTION             {4100}   
         9: SOLAR DASHBOARD        {4301}   
        10: KEYBOARD LAYOUT        {4520}   
     Has 12 reprogrammable keys:
         0: MY HOME                    => HomePage                      is FN, FN sensitive, reprogrammable
         1: Mail                       => Email                         is FN, FN sensitive, reprogrammable
         2: SEARCH                     => Search                        is FN, FN sensitive, reprogrammable
         3: Calculator                 => Calculator                    is FN, FN sensitive, reprogrammable
         4: MEDIA PLAYER               => Music                         is FN, FN sensitive, reprogrammable
         5: Previous                   => Previous                      is FN, FN sensitive
         6: Play/Pause                 => Play/Pause                    is FN, FN sensitive
         7: Next                       => Next                          is FN, FN sensitive
         8: Mute                       => Mute                          is FN, FN sensitive
         9: Volume Down                => Volume Down                   is FN, FN sensitive
        10: Volume Up                  => Volume Up                     is FN, FN sensitive
        11: SLEEP                      => Sleep                         is FN, FN sensitive, reprogrammable
     Battery status unavailable.

  4: Wireless Solar Keyboard K750
     Codename     : K750
     Kind         : keyboard
     Wireless PID : 4002
     Protocol     : HID++ 2.0
     Polling rate : 20 ms (50Hz)
     Serial number: A92EA88D
     The power switch is located on the edge of top right corner.
     Battery: unknown (device is offline).

Unifying Receiver
  Device path  : /dev/hidraw1
  USB id       : 046d:c525
  Serial       : 0C0D3AA5
    Firmware   : 02.00.B0020
  Has 1 paired device(s) out of a maximum of 1.
  Notifications: (none)

  1: MX Revolution
     Codename     : MX Revolution
     Kind         : mouse
     Wireless PID : 100C
     Protocol     : HID++ 1.0
     Serial number: 0C0D3AA5
     The power switch is located on the (unknown).
     Battery: unknown (device is offline).


```

**Describe the bug**
Observe that the screenshot below, says "good", yet the battery symbol is low, the LEDs on the mouse indicate good battery too.

**Screenshots**
![Screenshot from 2020-03-20 11-57-28](https://user-images.githubusercontent.com/1468842/77157807-629e2900-6aa2-11ea-8fb3-6fd52763bdaf.png)
![Screenshot from 2020-03-20 10-53-04](https://user-images.githubusercontent.com/1468842/77157815-65008300-6aa2-11ea-87a6-36aad6a28cb7.png)


**Additional context**
Thank you for Solaar


Discussion:
This probably the same problem as #608.  That issue was resolved by upgrading to the current version of Solaar from this repository.  Pease clone this repository and run bin/solaar from there to see if the battery icon problem persists.

Closing due to no response from the submitter.
Sorry for the late feedback.
Yes, 1.0.2-rc1 works great.
Correction: 
Please reopen this issue.
Solaar 1.0.2-rc1 having low battery alarm and the mouse, showing two of three LED's:

![20200413_003147](https://user-images.githubusercontent.com/1468842/79596158-59739c80-80e0-11ea-9053-d59dce040b7a.jpg)

Please provide output from `solaar show -dd`.

"-dd" is not accepted:
andre@loke:~$ solaar show -dd
usage: solaar {show,probe,config,pair,unpair} ...
solaar: error: unrecognized arguments: -dd


sudo solaar show 
[sudo] password for andre: 
Unifying Receiver
  Device path  : /dev/hidraw3
  USB id       : 046d:c52b
  Serial       : CD0A94D5
    Firmware   : 12.10.B0032
    Bootloader : 02.14
    Other      : AA.AA
  Has 3 paired device(s) out of a maximum of 6.
  Notifications: wireless (0x000100)
  Device activity counters: 1=84, 2=117

  1: Performance Mouse MX
     Codename     : Performance MX
     Kind         : mouse
     Wireless PID : 101A
     Protocol     : HID++ 1.0
     Polling rate : 8 ms (125Hz)
     Serial number: 01396843
          Firmware: 15.01.B0062
        Bootloader: 02.11
             Other: 00.09
     The power switch is located on the base.
     Notifications: battery status (0x100000).
     Battery: low, discharging.

  2: Wireless Solar Keyboard K750
     Codename     : K750
     Kind         : keyboard
     Wireless PID : 4002
     Protocol     : HID++ 2.0
     Polling rate : 20 ms (50Hz)
     Serial number: 72D5DF87
          Firmware: RQK 33.00.B0015
        Bootloader: DFU 00.02.B0003
     The power switch is located on the edge of top right corner.
     Supports 11 HID++ 2.0 features:
         0: ROOT                   {0000}   
         1: FEATURE SET            {0001}   
         2: DEVICE FW VERSION      {0003}   
         3: DEVICE NAME            {0005}   
         4: REPROG CONTROLS        {1B00}   
         5: WIRELESS DEVICE STATUS {1D4B}   
         6: unknown:1DF3           {1DF3}   hidden
         7: FN INVERSION           {40A0}   
         8: ENCRYPTION             {4100}   
         9: SOLAR DASHBOARD        {4301}   
        10: KEYBOARD LAYOUT        {4520}   
     Has 12 reprogrammable keys:
         0: MY HOME                    => HomePage                      is FN, FN sensitive, reprogrammable
         1: Mail                       => Email                         is FN, FN sensitive, reprogrammable
         2: SEARCH                     => Search                        is FN, FN sensitive, reprogrammable
         3: Calculator                 => Calculator                    is FN, FN sensitive, reprogrammable
         4: MEDIA PLAYER               => Music                         is FN, FN sensitive, reprogrammable
         5: Previous                   => Previous                      is FN, FN sensitive
         6: Play/Pause                 => Play/Pause                    is FN, FN sensitive
         7: Next                       => Next                          is FN, FN sensitive
         8: Mute                       => Mute                          is FN, FN sensitive
         9: Volume Down                => Volume Down                   is FN, FN sensitive
        10: Volume Up                  => Volume Up                     is FN, FN sensitive
        11: SLEEP                      => Sleep                         is FN, FN sensitive, reprogrammable
     Battery status unavailable.

  4: Wireless Solar Keyboard K750
     Codename     : K750
     Kind         : keyboard
     Wireless PID : 4002
     Protocol     : HID++ 2.0
     Polling rate : 20 ms (50Hz)
     Serial number: A92EA88D
     The power switch is located on the edge of top right corner.
     Battery: unknown (device is offline).

Nano Receiver
  Device path  : /dev/hidraw1
  USB id       : 046d:c525
  Serial       : 0C0D3AA5
    Firmware   : 02.00.B0020
  Has 1 paired device(s) out of a maximum of 1.
  Notifications: (none)

  1: MX Revolution
     Codename     : MX Revolution
     Kind         : mouse
     Wireless PID : 100C
     Protocol     : HID++ 1.0
     Serial number: 0C0D3AA5
     The power switch is located on the (unknown).
     Battery: unknown (device is offline).


Oops, it's `solaar -dd show`  Sorry.
no problem, thanks for looking into this.
please see https://gist.github.com/AndKe/9d941444ba0f85ed1fba1a40b709c60f
OK that log is useful.  Here is the relevant part
```
20:17:44,805    DEBUG [MainThread] logitech_receiver.base: (3) => r[10 01 8107 030000]
Battery: low, discharging.
```
The problem in solaar is that low is turned into 20% and then to the caution icon.  I'll see if something better can be done.

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
@@ -120,6 +120,11 @@ def battery(level=None, charging=False):
 	# 	_log.debug("battery icon for %s:%s = %s", level, charging, icon_name)
 	return icon_name
 
+# return first res where val >= guard
+# _first_res(val,((guard,res),...))
+def _first_res(val,pairs):
+	return next((res for guard,res in pairs if val >= guard),None)
+
 def _battery_icon_name(level, charging):
 	_init_icon_paths()
 
@@ -135,7 +140,7 @@ def _battery_icon_name(level, charging):
 	if _has_mint_icons:
 		if level == 100 and charging:
 			return 'battery-full-charged-symbolic'
-		level_name = ('empty', 'caution', 'low', 'good', 'good', 'full')[level_approx // 20]
+		level_name = _first_res(level,((90,'full'), (50,'good'), (20,'low'), (5,'caution'), (0,'empty')))
 		return 'battery-%s%s-symbolic' % (level_name, '-charging' if charging else '')
 
 	if _has_gpm_icons:
@@ -146,7 +151,7 @@ def _battery_icon_name(level, charging):
 	if _has_oxygen_icons:
 		if level_approx == 100 and charging:
 			return 'battery-charging'
-		level_name = ('low', 'caution', '040', '060', '080', '100')[level_approx // 20]
+		level_name = _first_res(level,((90,'100'), (75,'080'), (55,'060'), (35,'040'), (15,'low'), (0,'caution')))
 		return 'battery%s-%s' % ('-charging' if charging else '', level_name)
 
 	if _has_elementary_icons:
@@ -159,7 +164,7 @@ def _battery_icon_name(level, charging):
 			return 'battery-full-charged'
 		if level_approx == 0 and charging:
 			return 'battery-caution-charging'
-		level_name = ('empty', 'caution', 'low', 'good', 'good', 'full')[level_approx // 20]
+		level_name = _first_res(level,((90,'full'), (50,'good'), (20,'low'), (5,'caution'), (0,'empty')))
 		return 'battery-%s%s' % (level_name, '-charging' if charging else '')
 
 	# fallback... most likely will fail


Test patch: