Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from mankoell/battery_module
Battery module
- Loading branch information
Showing
13 changed files
with
177 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/python2 | ||
# -*- coding: utf-8 -*- | ||
|
||
from memacs.battery import Battery | ||
|
||
|
||
PROG_VERSION_NUMBER = u"0.1" | ||
PROG_VERSION_DATE = u"2017-02-24" | ||
PROG_SHORT_DESCRIPTION = u"Memacs for battery" | ||
PROG_TAG = u"battery" | ||
|
||
COPYRIGHT_YEAR = "2017" | ||
COPYRIGHT_AUTHORS = """Manuel Koell <mankoell@gmail.com>""" | ||
|
||
if __name__ == "__main__": | ||
memacs = Battery( | ||
prog_version=PROG_VERSION_NUMBER, | ||
prog_version_date=PROG_VERSION_DATE, | ||
prog_short_description=PROG_SHORT_DESCRIPTION, | ||
prog_tag=PROG_TAG, | ||
copyright_year=COPYRIGHT_YEAR, | ||
copyright_authors=COPYRIGHT_AUTHORS | ||
) | ||
memacs.handle_main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
## This file is best viewed with GNU Emacs Org-mode: http://orgmode.org/ | ||
|
||
* memacs-battery | ||
|
||
** Options | ||
|
||
- ~-b, --battery~ select battery to read stats from, default ~BAT0~ | ||
- ~--output-format~ format string to use for the output, see [[https://github.com/nicolargo/batinfo][batinfo]] default ~{battery.name}~ | ||
|
||
** Example | ||
|
||
: memacs_battery.py --battery BAT1 --output-format "{battery.name} {battery.status}" | ||
|
||
#+BEGIN_EXAMPLE | ||
* Memacs for battery :Memacs:battery: | ||
** <2017-02-24 Fri 12:20> BAT1 discharging | ||
:PROPERTIES: | ||
:STATUS: discharging | ||
:CAPACITY: 88% | ||
:CYCLE_COUNT: 866 | ||
:CONSUMPTION: 12.5 W | ||
:ID: 5248c6260f05f307533203be97e53cd38f8e628c | ||
:END: | ||
#+END_EXAMPLE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!/usr/bin/python2 | ||
# -*- coding: utf-8 -*- | ||
|
||
import argparse | ||
import datetime | ||
import logging | ||
import sys | ||
|
||
import batinfo | ||
|
||
from lib.orgproperty import OrgProperties | ||
from lib.orgformat import OrgFormat | ||
from lib.memacs import Memacs | ||
|
||
ROOT = '/sys/class/power_supply' | ||
|
||
|
||
class Battery(Memacs): | ||
def _parser_add_arguments(self): | ||
""" | ||
overwritten method of class Memacs | ||
add additional arguments | ||
""" | ||
Memacs._parser_add_arguments(self) | ||
|
||
self._parser.add_argument( | ||
"-b", "--battery", dest="name", | ||
action="store", default="BAT0", | ||
help="select battery to read stats from") | ||
|
||
self._parser.add_argument( | ||
"-p", "--path", dest="path", | ||
action="store", default=ROOT, | ||
help=argparse.SUPPRESS) | ||
|
||
self._parser.add_argument( | ||
"--output-format", dest="output_format", | ||
action="store", default="{battery.name}", | ||
help="format string to use for the output" | ||
) | ||
|
||
def _parser_parse_args(self): | ||
""" | ||
overwritten method of class Memacs | ||
all additional arguments are parsed in here | ||
""" | ||
Memacs._parser_parse_args(self) | ||
|
||
def _handle_battery(self, bat): | ||
""" | ||
handle single battery, e.g. BAT0 | ||
""" | ||
|
||
# calculate watt usage | ||
consumption = float(bat.current_now / 1000000.0 * | ||
bat.voltage_now / 1000000.0) | ||
|
||
timestamp = OrgFormat.datetime(datetime.datetime.now()) | ||
output = self._args.output_format.format(battery=bat) | ||
|
||
properties = OrgProperties(data_for_hashing=timestamp) | ||
properties.add("CYCLE_COUNT", bat.cycle_count) | ||
properties.add("CAPACITY", '%s%%' % bat.capacity) | ||
properties.add("STATUS", bat.status.lower()) | ||
|
||
if consumption: | ||
properties.add("CONSUMPTION", '%.1f W' % consumption) | ||
|
||
self._writer.write_org_subitem(timestamp=timestamp, | ||
output=output, | ||
properties=properties) | ||
|
||
def _main(self): | ||
""" | ||
get's automatically called from Memacs class | ||
""" | ||
|
||
try: | ||
batteries = batinfo.Batteries(self._args.path) | ||
|
||
for bat in batteries.stat: | ||
if self._args.name in bat.name: | ||
self._handle_battery(bat) | ||
|
||
except OSError, e: | ||
logging.error("no battery present") | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import unittest | ||
import os | ||
|
||
from memacs.battery import Battery | ||
|
||
|
||
class TestCsv(unittest.TestCase): | ||
|
||
def test_battery(self): | ||
|
||
path = os.path.dirname(os.path.abspath(__file__)) | ||
|
||
argv = [] | ||
argv.append("-p") | ||
argv.append(os.path.join(path, "data")) | ||
|
||
memacs = Battery(argv=argv) | ||
data = memacs.test_get_entries() | ||
|
||
self.assertEqual(data[1], " :PROPERTIES:") | ||
self.assertEqual(data[2], " :STATUS: discharging") | ||
self.assertEqual(data[3], " :CAPACITY: 97%") | ||
self.assertEqual(data[4], " :CYCLE_COUNT: 866") | ||
self.assertEqual(data[5], " :CONSUMPTION: 11.9 W") | ||
self.assertEqual(data[7], " :END:") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
97 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
955000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Discharging |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Battery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
12457000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,5 @@ Pillow==2.4.0 | |
argparse==1.2.1 | ||
feedparser==5.1.2 | ||
icalendar==3.1 | ||
|
||
|
||
|
||
twython | ||
batinfo |