Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from pi3bar.plugins.base import Plugin 

2 

3 

4class Battery(Plugin): 

5 """ 

6 :class:`pi3bar.app.Pi3Bar` plugin to show battery charge. 

7 

8 :param bat_name: :class:`str` - Battery name in */sys/class/power_supply/* (default: 'BAT0') 

9 :param warning_capacity: :class:`int` - Warning breakpoint (default: 30) 

10 :param warning_color: :class:`str` - Warning color (default: '#ffff00') 

11 :param warning_background: :class:`str` - Warning background color (default: None) 

12 :param critical_capacity: :class:`int` - Critical breakpoint (default: 15) 

13 :param critical_color: :class:`str` - Critical color (default: None) 

14 :param critical_background: :class:`str` - Critical background color (default: '#ff0000') 

15 

16 Examples: 

17 

18 .. code-block:: python 

19 

20 Battery() 

21 

22 # sometimes BAT0 is another battery, use BAT1 instead: 

23 Battery('BAT1') 

24 

25 # change warning/critical capacity: 

26 Battery('BAT1', warning_capacity=30, critical_capacity=15) 

27 

28 # change colors: 

29 Battery(critical_color='#ff0000', critical_background=None) 

30 """ 

31 

32 #: Refresh every 10 seconds 

33 ticks = 10 

34 

35 def __init__(self, bat_name='BAT0', **kwargs): 

36 self.capacity_path = '/sys/class/power_supply/%s/capacity' % bat_name 

37 self.status_path = '/sys/class/power_supply/%s/status' % bat_name 

38 self.warning_capacity = kwargs.pop('warning_capacity', 30) 

39 self.warning_color = kwargs.pop('warning_color', '#ffff00') 

40 self.warning_background = kwargs.pop('warning_background', None) 

41 self.critical_capacity = kwargs.pop('critical_capacity', 15) 

42 self.critical_color = kwargs.pop('critical_color', None) 

43 self.critical_background = kwargs.pop('critical_background', '#ff0000') 

44 super(Battery, self).__init__(**kwargs) 

45 

46 def get_capacity(self): 

47 """ 

48 Read battery capacity file and return the content. 

49 

50 :return: :class:`int` - battery capacity percentage 

51 """ 

52 with open(self.capacity_path, 'r') as f: 

53 line = f.readline() 

54 capacity = line.rstrip('\n') 

55 return int(capacity) 

56 

57 def get_status(self): 

58 """ 

59 Read battery status file and return the content. 

60 

61 :return: :class:`str` - battery status 

62 """ 

63 with open(self.status_path, 'r') as f: 

64 return f.readline().rstrip('\n') 

65 

66 def cycle(self): 

67 capacity = self.get_capacity() 

68 status = self.get_status() 

69 

70 self.full_text = '%s %d%%' % (status, capacity) 

71 self.short_text = '%s%%' % capacity 

72 

73 if capacity < self.critical_capacity: 

74 self.color = self.critical_color 

75 self.background = self.critical_background 

76 

77 elif capacity < self.warning_capacity: 

78 self.color = self.warning_color 

79 self.background = self.warning_background 

80 

81 else: 

82 self.color = None 

83 self.background = None 

84