Fix type
Error for check type of file Add tests
This commit is contained in:
@@ -22,7 +22,7 @@ class Battery:
|
||||
def find_uevent(batteries: List[str]) -> Union[Path, None]:
|
||||
for battery in batteries:
|
||||
uevent_path = POWER_CLASS.joinpath(battery, "uevent")
|
||||
if uevent_path.is_fifo():
|
||||
if uevent_path.is_file():
|
||||
return uevent_path
|
||||
return None
|
||||
|
||||
@@ -35,14 +35,26 @@ def read_uevent(filename: Path) -> Dict[str, str]:
|
||||
|
||||
|
||||
def battery_info(uevent: Dict[str, str]) -> Battery:
|
||||
power_full = [
|
||||
"POWER_SUPPLY_ENERGY_FULL_DESIGN"
|
||||
"POWER_SUPPLY_ENERGY_FULL",
|
||||
"POWER_SUPPLY_CHARGE_FULL",
|
||||
]
|
||||
return Battery(
|
||||
name=uevent.get("POWER_SUPPLY_NAME", "??"),
|
||||
model=uevent.get("POWER_SUPPLY_MODEL_NAME", ""),
|
||||
manufacturer=uevent.get("POWER_SUPPLY_MANUFACTURER", ""),
|
||||
technology=uevent.get("POWER_SUPPLY_TECHNOLOGY", ""),
|
||||
capacity=float(uevent.get("POWER_SUPPLY_CHARGE_FULL", 0)) / 1e6,
|
||||
capacity=float(find_first(uevent, power_full, "0")) / 1e6,
|
||||
percent=int(uevent.get("POWER_SUPPLY_CAPACITY", 0)),
|
||||
status=uevent.get("POWER_SUPPLY_STATUS", "??"),
|
||||
time_to_empty=int(uevent.get("POWER_SUPPLY_TIME_TO_EMPTY_NOW", 0)),
|
||||
current=int(float(uevent.get("POWER_SUPPLY_CURRENT_NOW", 0)) / 1e3),
|
||||
)
|
||||
|
||||
|
||||
def find_first(uevent: Dict[str, str], keys: List[str], default: str) -> str:
|
||||
for key in keys:
|
||||
if key in uevent:
|
||||
return uevent[key]
|
||||
return default
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from battery.power import Battery, battery_info, read_uevent
|
||||
from battery.power import Battery, battery_info, read_uevent, find_first
|
||||
|
||||
|
||||
def load_fixture(filename: str) -> dict[str, str]:
|
||||
@@ -81,3 +81,11 @@ class TestPower(unittest.TestCase):
|
||||
uevent = load_fixture(filename)
|
||||
info = battery_info(uevent)
|
||||
self.assertEqual(info, expected_info)
|
||||
|
||||
def test_find_first_default(self) -> None:
|
||||
uevent = {"ONE": 1, "TWO": 2, "THREE": 3}
|
||||
self.assertEqual(find_first(uevent, ["XX", "YY"], 10), 10)
|
||||
|
||||
def test_find_first_more_values(self) -> None:
|
||||
uevent = {"ONE": 1, "TWO": 2, "THREE": 3}
|
||||
self.assertEqual(find_first(uevent, ["TWO", "ONE", "XX"], 10), 2)
|
||||
|
||||
Reference in New Issue
Block a user