|
9 | 9 | from homeassistant.components.binary_sensor import BinarySensorDeviceClass |
10 | 10 |
|
11 | 11 | from custom_components.pettracer.const import DOMAIN |
12 | | -from custom_components.pettracer.binary_sensor import PetTracerAtHomeBinarySensor |
| 12 | +from custom_components.pettracer.binary_sensor import ( |
| 13 | + PetTracerAtHomeBinarySensor, |
| 14 | + PetTracerChargingBinarySensor, |
| 15 | +) |
13 | 16 |
|
14 | 17 |
|
15 | 18 | async def test_binary_sensor_setup(hass, mock_pettracer_client_init, mock_device): |
@@ -48,8 +51,9 @@ def mock_add_entities(new_entities, update_before_add): |
48 | 51 |
|
49 | 52 | await binary_sensor_setup(hass, entry, mock_add_entities) |
50 | 53 |
|
51 | | - assert len(entities) == 1 |
| 54 | + assert len(entities) == 2 |
52 | 55 | assert isinstance(entities[0], PetTracerAtHomeBinarySensor) |
| 56 | + assert isinstance(entities[1], PetTracerChargingBinarySensor) |
53 | 57 |
|
54 | 58 |
|
55 | 59 | async def test_at_home_binary_sensor_true(hass, mock_device): |
@@ -105,3 +109,58 @@ async def test_at_home_binary_sensor_device_info(hass, mock_device): |
105 | 109 | assert device_info["name"] == "Fluffy" |
106 | 110 | assert device_info["manufacturer"] == "PetTracer" |
107 | 111 | assert device_info["model"] == "GPS Collar" |
| 112 | + |
| 113 | + |
| 114 | +async def test_charging_binary_sensor_true(hass, mock_device): |
| 115 | + """Test charging binary sensor when collar is charging.""" |
| 116 | + coordinator = MagicMock() |
| 117 | + coordinator.data = {"devices": [mock_device]} |
| 118 | + |
| 119 | + sensor = PetTracerChargingBinarySensor(coordinator, mock_device) |
| 120 | + |
| 121 | + assert sensor.unique_id == "pettracer_12345_charging" |
| 122 | + assert sensor.name == "Fluffy Charging" |
| 123 | + assert sensor.device_class == BinarySensorDeviceClass.BATTERY_CHARGING |
| 124 | + assert sensor.is_on is True |
| 125 | + |
| 126 | + |
| 127 | +async def test_charging_binary_sensor_false(hass, mock_device_no_position): |
| 128 | + """Test charging binary sensor when collar is not charging.""" |
| 129 | + coordinator = MagicMock() |
| 130 | + coordinator.data = {"devices": [mock_device_no_position]} |
| 131 | + |
| 132 | + sensor = PetTracerChargingBinarySensor(coordinator, mock_device_no_position) |
| 133 | + |
| 134 | + assert sensor.unique_id == "pettracer_12346_charging" |
| 135 | + assert sensor.name == "Rex Charging" |
| 136 | + assert sensor.is_on is False |
| 137 | + |
| 138 | + |
| 139 | +async def test_charging_binary_sensor_none(hass): |
| 140 | + """Test charging binary sensor when chg is None.""" |
| 141 | + device = MagicMock() |
| 142 | + device.id = 99999 |
| 143 | + device.details = None |
| 144 | + device.chg = None |
| 145 | + device.sw = None |
| 146 | + |
| 147 | + coordinator = MagicMock() |
| 148 | + coordinator.data = {"devices": [device]} |
| 149 | + |
| 150 | + sensor = PetTracerChargingBinarySensor(coordinator, device) |
| 151 | + |
| 152 | + assert sensor.is_on is None |
| 153 | + |
| 154 | + |
| 155 | +async def test_charging_binary_sensor_device_info(hass, mock_device): |
| 156 | + """Test charging binary sensor device info.""" |
| 157 | + coordinator = MagicMock() |
| 158 | + coordinator.data = {"devices": [mock_device]} |
| 159 | + |
| 160 | + sensor = PetTracerChargingBinarySensor(coordinator, mock_device) |
| 161 | + device_info = sensor.device_info |
| 162 | + |
| 163 | + assert device_info["identifiers"] == {(DOMAIN, 12345)} |
| 164 | + assert device_info["name"] == "Fluffy" |
| 165 | + assert device_info["manufacturer"] == "PetTracer" |
| 166 | + assert device_info["model"] == "GPS Collar" |
0 commit comments