@@ -85,10 +85,9 @@ async def test_battery_sensor(hass, mock_device):
8585 assert sensor .native_unit_of_measurement == "%"
8686 assert sensor .state_class == SensorStateClass .MEASUREMENT
8787
88- # Test battery conversion (4100mV should be around 91 %)
88+ # Test battery conversion (4100mV should be 100 %)
8989 battery = sensor .native_value
90- assert battery is not None
91- assert 80 <= battery <= 95
90+ assert battery == 100
9291
9392
9493async def test_battery_voltage_sensor (hass , mock_device ):
@@ -422,3 +421,45 @@ async def test_sensor_no_details(hass):
422421
423422 device_info = sensor .device_info
424423 assert device_info ["sw_version" ] is None
424+
425+
426+ async def test_battery_percentage_edge_cases (hass , mock_device ):
427+ """Test battery percentage calculation with edge cases."""
428+ from custom_components .pettracer .sensor import PetTracerBatterySensor
429+
430+ coordinator = MagicMock ()
431+ coordinator .data = {"devices" : [mock_device ]}
432+
433+ sensor = PetTracerBatterySensor (coordinator , mock_device )
434+
435+ # Test minimum voltage (3600mV = 0%)
436+ mock_device .bat = 3600
437+ assert sensor .native_value == 0
438+
439+ # Test maximum voltage (4100mV = 100%)
440+ mock_device .bat = 4100
441+ assert sensor .native_value == 100
442+
443+ # Test below minimum (should cap at 0%)
444+ mock_device .bat = 3500
445+ assert sensor .native_value == 0
446+
447+ # Test above maximum (should cap at 100%)
448+ mock_device .bat = 4200
449+ assert sensor .native_value == 100
450+
451+ # Test mid-range (3850mV = 50%)
452+ mock_device .bat = 3850
453+ assert sensor .native_value == 50
454+
455+ # Test 3800mV (should be 40%)
456+ mock_device .bat = 3800
457+ assert sensor .native_value == 40
458+
459+ # Test 3900mV (should be 60%)
460+ mock_device .bat = 3900
461+ assert sensor .native_value == 60
462+
463+ # Test 4000mV (should be 80%)
464+ mock_device .bat = 4000
465+ assert sensor .native_value == 80
0 commit comments