def test_sensors_power_unknown_cap(tmp_path: pathlib.Path) -> None: populate_directory( str(tmp_path), { "class": { "power_supply": { "BAT0": { "type": "Battery\n", "status": "full", "capacity": "100", "energy_now": "10000", }, "BAT1": { "type": "Battery\n", "status": "full", "capacity": "100", "energy_now": "10000", }, "AC0": { "type": "Mains\n", "online": "1", }, }, }, }, ) with replace_info_directories(sysfs=str(tmp_path)): assert pypsutil.sensors_power( ) == pypsutil.PowerSupplySensorInfo( # type: ignore batteries=[ pypsutil.BatteryInfo( name="BAT0", status=pypsutil.BatteryStatus.FULL, percent=100, energy_now=10000, energy_full=None, power_now=None, ), pypsutil.BatteryInfo( name="BAT1", status=pypsutil.BatteryStatus.FULL, percent=100, energy_now=10000, energy_full=None, power_now=None, ), ], ac_supplies=[pypsutil.ACPowerInfo(name="AC0", is_online=True)], ) assert pypsutil.sensors_battery( ) == pypsutil.BatteryInfo( # type: ignore name="BAT0", status=pypsutil.BatteryStatus.FULL, percent=100, energy_now=10000, energy_full=None, power_now=None, _power_plugged=True, ) # Unable to determine total capacity assert pypsutil.sensors_battery_total() is None # type: ignore assert pypsutil.sensors_is_on_ac_power() is True # type: ignore
def test_sensors_battery(tmp_path: pathlib.Path) -> None: def test_info( ps_info: Dict[str, Dict[str, str]], battery_info: Optional[pypsutil.BatteryInfo], ) -> None: populate_directory(str(tmp_path), {"class": {"power_supply": ps_info}}) assert pypsutil.sensors_battery() == battery_info # type: ignore shutil.rmtree(tmp_path / "class") with replace_info_directories(sysfs=str(tmp_path)): test_info( { "BAT0": { "type": "Battery\n", "status": "full", "capacity": "100", "charge_now": "10000", "charge_full": "10000", }, "AC0": { "type": "Mains\n", "online": "1", }, }, pypsutil.BatteryInfo( name="BAT0", percent=100, energy_now=None, energy_full=None, power_now=None, status=pypsutil.BatteryStatus.FULL, _power_plugged=True, ), ) test_info( { "BAT0": { "type": "Battery\n", "status": "unknown", "power_now": "100", "energy_now": "10000", "energy_full": "10000", }, }, pypsutil.BatteryInfo( name="BAT0", percent=100, power_now=100, energy_now=10000, energy_full=10000, status=pypsutil.BatteryStatus.UNKNOWN, _power_plugged=None, ), ) test_info( { "BAT0": { "type": "Battery\n", "status": "unknown", "power_now": "100", "energy_now": "10000", "energy_full": "10000", }, "AC0": { "type": "Mains\n", "online": "1", }, }, pypsutil.BatteryInfo( name="BAT0", percent=100, power_now=100, energy_now=10000, energy_full=10000, status=pypsutil.BatteryStatus.UNKNOWN, _power_plugged=True, ), ) test_info( { "BAT0": { "type": "Battery\n", "status": "unknown", "power_now": "100", "energy_now": "10000", "energy_full": "10000", }, "AC0": { "type": "Mains\n", "online": "0", }, }, pypsutil.BatteryInfo( name="BAT0", percent=100, power_now=100, energy_now=10000, energy_full=10000, status=pypsutil.BatteryStatus.UNKNOWN, _power_plugged=False, ), ) test_info( { "BAT0": { "type": "Battery\n", "status": "discharging", "energy_now": "10000", "energy_full": "10000", }, }, pypsutil.BatteryInfo( name="BAT0", percent=100, power_now=None, energy_now=10000, energy_full=10000, status=pypsutil.BatteryStatus.DISCHARGING, ), ) test_info( { "BAT0": { "type": "Battery\n", "status": "charging", "energy_now": "10000", "energy_full": "10000", }, }, pypsutil.BatteryInfo( name="BAT0", percent=100, power_now=None, energy_now=10000, energy_full=10000, status=pypsutil.BatteryStatus.CHARGING, ), )
def test_battery_info() -> None: binfo = pypsutil.BatteryInfo( name="BAT0", status=pypsutil.BatteryStatus.FULL, percent=100.0, energy_full=None, energy_now=None, power_now=None, ) assert binfo.power_plugged is None assert binfo.secsleft is not None assert binfo.secsleft > 0 assert math.isinf(binfo.secsleft) assert binfo.secsleft_full == 0.0 assert ( str(binfo) == "BatteryInfo(name='BAT0', status=<BatteryStatus.FULL: 'full'>, " "power_plugged=None, percent=100.0, secsleft=inf, secsleft_full=0.0)" ) binfo = pypsutil.BatteryInfo( name="BAT0", status=pypsutil.BatteryStatus.CHARGING, percent=90.0, energy_full=None, energy_now=None, power_now=None, ) assert binfo.power_plugged is True assert binfo.secsleft is not None assert binfo.secsleft > 0 assert math.isinf(binfo.secsleft) assert binfo.secsleft_full is None assert ( str(binfo) == "BatteryInfo(name='BAT0', status=<BatteryStatus.CHARGING: 'charging'>, " "power_plugged=True, percent=90.0, secsleft=inf, secsleft_full=None)" ) binfo = pypsutil.BatteryInfo( name="BAT0", status=pypsutil.BatteryStatus.CHARGING, percent=90.0, energy_full=100, energy_now=100, power_now=0, ) assert binfo.power_plugged is True assert binfo.secsleft is not None assert binfo.secsleft > 0 assert math.isinf(binfo.secsleft) assert binfo.secsleft_full is None assert ( str(binfo) == "BatteryInfo(name='BAT0', status=<BatteryStatus.CHARGING: 'charging'>, " "power_plugged=True, percent=90.0, secsleft=inf, secsleft_full=None)" ) binfo = pypsutil.BatteryInfo( name="BAT0", status=pypsutil.BatteryStatus.CHARGING, percent=90.0, energy_full=100, energy_now=90, power_now=10, ) assert binfo.power_plugged is True assert binfo.secsleft is not None assert binfo.secsleft > 0 assert math.isinf(binfo.secsleft) assert binfo.secsleft_full == 3600.0 assert ( str(binfo) == "BatteryInfo(name='BAT0', status=<BatteryStatus.CHARGING: 'charging'>, " "power_plugged=True, percent=90.0, secsleft=inf, secsleft_full=3600.0)" ) binfo = pypsutil.BatteryInfo( name="BAT0", status=pypsutil.BatteryStatus.DISCHARGING, percent=90.0, energy_full=100, energy_now=90, power_now=10, ) assert binfo.power_plugged is False assert binfo.secsleft == 3600 * 9 assert binfo.secsleft_full is None assert ( str(binfo) == "BatteryInfo(name='BAT0', status=<BatteryStatus.DISCHARGING: 'discharging'>, " "power_plugged=False, percent=90.0, secsleft=32400.0, secsleft_full=None)" ) binfo = pypsutil.BatteryInfo( name="BAT0", status=pypsutil.BatteryStatus.DISCHARGING, percent=90.0, energy_full=100, energy_now=90, power_now=0, ) assert binfo.power_plugged is False assert binfo.secsleft is None assert binfo.secsleft_full is None assert ( str(binfo) == "BatteryInfo(name='BAT0', status=<BatteryStatus.DISCHARGING: 'discharging'>, " "power_plugged=False, percent=90.0, secsleft=None, secsleft_full=None)" )
def test_sensors_power(tmp_path: pathlib.Path) -> None: populate_directory( str(tmp_path), { "class": { "power_supply": { "BAT0": { "type": "Battery\n", "status": "full", "capacity": "100", "charge_now": "10000", "charge_full": "10000", }, "BAT1": { "type": "Battery\n", "uevent": build_supply_uevent({ "status": "Charging", "capacity": "90", "energy_now": "899", "energy_full": "1000", }), }, "BAT2": { "type": "Battery\n", "uevent": build_supply_uevent({ "status": "Discharging", "capacity": "70", "charge_now": "700", "charge_full": "1000", "current_now": "350", "voltage_now": "2000000", }), }, "BAT3": { "type": "Battery\n", "uevent": build_supply_uevent({ "status": "Charging", "capacity": "70", "energy_now": "700", "energy_full": "1000", "power_now": "300", }), }, "BAT4": { "type": "Battery\n", "status": "Discharging", "energy_now": "700", "energy_full": "1000", "power_now": "350", }, "BAT5": { "type": "Battery\n", "status": "Unknown", "energy_now": "700", "energy_full": "1000", "power_now": "350", }, "BAT6": { "type": "Battery\n", "status": "Charging", "charge_now": "700", "charge_full": "1000", "current_now": "300", "voltage_now": "1000000", }, # Will be ignored for lack of information "BAT7": { "type": "Battery\n", "status": "Charging", }, "AC0": { "type": "Mains\n", "online": "1", }, "AC1": { # The "online=1" line should be ignored "uevent": "POWER_SUPPLY_TYPE=Mains\nPOWER_SUPPLY_ONLINE=0\nonline=1\n", }, # Will be ignored for lack of information "AC2": { "type": "Mains\n", }, }, }, }, ) with replace_info_directories(sysfs=str(tmp_path)): psinfo = pypsutil.sensors_power() # type: ignore assert psinfo.batteries == [ pypsutil.BatteryInfo( name="BAT0", percent=100.0, status=pypsutil.BatteryStatus.FULL, energy_now=None, energy_full=None, power_now=None, ), pypsutil.BatteryInfo( name="BAT1", percent=89.9, status=pypsutil.BatteryStatus.CHARGING, energy_now=899, energy_full=1000, power_now=None, ), pypsutil.BatteryInfo( name="BAT2", percent=70, status=pypsutil.BatteryStatus.DISCHARGING, energy_now=1400, energy_full=2000, power_now=700, ), pypsutil.BatteryInfo( name="BAT3", percent=70, status=pypsutil.BatteryStatus.CHARGING, energy_now=700, energy_full=1000, power_now=300, ), pypsutil.BatteryInfo( name="BAT4", percent=70, status=pypsutil.BatteryStatus.DISCHARGING, energy_now=700, energy_full=1000, power_now=350, ), pypsutil.BatteryInfo( name="BAT5", percent=70, status=pypsutil.BatteryStatus.UNKNOWN, energy_now=700, energy_full=1000, power_now=350, ), pypsutil.BatteryInfo( name="BAT6", percent=70, status=pypsutil.BatteryStatus.CHARGING, energy_now=700, energy_full=1000, power_now=300, ), ] assert pypsutil.sensors_battery_total( ) == pypsutil.BatteryInfo( # type: ignore name="Combined", percent=5099 * 100 / 7000, status=pypsutil.BatteryStatus.UNKNOWN, energy_now=5099, energy_full=7000, power_now=None, _power_plugged=True, ) assert psinfo.ac_supplies == [ pypsutil.ACPowerInfo(name="AC0", is_online=True), pypsutil.ACPowerInfo(name="AC1", is_online=False), ] assert pypsutil.sensors_is_on_ac_power() is True # type: ignore
def test_ps_sensor_info() -> None: bat_full = pypsutil.BatteryInfo( name="BAT0", status=pypsutil.BatteryStatus.FULL, percent=100.0, energy_full=None, energy_now=None, power_now=None, ) bat_charging = pypsutil.BatteryInfo( name="BAT1", status=pypsutil.BatteryStatus.CHARGING, percent=90.0, energy_full=None, energy_now=None, power_now=None, ) bat_discharging = pypsutil.BatteryInfo( name="BAT2", status=pypsutil.BatteryStatus.DISCHARGING, percent=90.0, energy_full=None, energy_now=None, power_now=None, ) bat_unknown = pypsutil.BatteryInfo( name="BAT3", status=pypsutil.BatteryStatus.UNKNOWN, percent=90.0, energy_full=None, energy_now=None, power_now=None, ) ac_online = pypsutil.ACPowerInfo(name="ADP1", is_online=True) ac_offline = pypsutil.ACPowerInfo(name="ADP2", is_online=False) psinfo = pypsutil.PowerSupplySensorInfo(batteries=[], ac_supplies=[]) assert psinfo.is_on_ac_power is None psinfo = pypsutil.PowerSupplySensorInfo(batteries=[bat_full, bat_unknown], ac_supplies=[]) assert psinfo.is_on_ac_power is None psinfo = pypsutil.PowerSupplySensorInfo( batteries=[bat_full, bat_unknown], ac_supplies=[ac_offline, ac_offline] ) assert psinfo.is_on_ac_power is False psinfo = pypsutil.PowerSupplySensorInfo( batteries=[bat_full, bat_unknown], ac_supplies=[ac_online] ) assert psinfo.is_on_ac_power is True psinfo = pypsutil.PowerSupplySensorInfo( batteries=[bat_full, bat_unknown], ac_supplies=[ac_offline, ac_online] ) assert psinfo.is_on_ac_power is True psinfo = pypsutil.PowerSupplySensorInfo(batteries=[bat_charging], ac_supplies=[]) assert psinfo.is_on_ac_power is True psinfo = pypsutil.PowerSupplySensorInfo(batteries=[bat_discharging], ac_supplies=[]) assert psinfo.is_on_ac_power is False psinfo = pypsutil.PowerSupplySensorInfo(batteries=[bat_full], ac_supplies=[]) assert psinfo.is_on_ac_power is True