Skip to content

Commit

Permalink
feat: add binary_sensor.<car>_asleep (#361)
Browse files Browse the repository at this point in the history
closes #360
  • Loading branch information
carleeno authored Nov 20, 2022
1 parent 6a1b3da commit 43deefb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions custom_components/tesla_custom/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
for car in cars.values():
entities.append(TeslaCarParkingBrake(hass, car, coordinator))
entities.append(TeslaCarOnline(hass, car, coordinator))
entities.append(TeslaCarAsleep(hass, car, coordinator))
entities.append(TeslaCarChargerConnection(hass, car, coordinator))
entities.append(TeslaCarCharging(hass, car, coordinator))

Expand Down Expand Up @@ -140,9 +141,30 @@ def extra_state_attributes(self):
"vehicle_id": str(self._car.vehicle_id),
"vin": self._car.vin,
"id": str(self._car.id),
"state": self._car.state,
}


class TeslaCarAsleep(TeslaCarEntity, BinarySensorEntity):
"""Representation of a Tesla car asleep binary sensor."""

def __init__(
self,
hass: HomeAssistant,
car: TeslaCar,
coordinator: TeslaDataUpdateCoordinator,
) -> None:
"""Initialize car asleep entity."""
super().__init__(hass, car, coordinator)
self.type = "asleep"
self._attr_device_class = None

@property
def is_on(self):
"""Return True if car is asleep."""
return self._car.state == "asleep"


class TeslaEnergyBatteryCharging(TeslaEnergyEntity, BinarySensorEntity):
"""Representation of a Tesla energy charging binary sensor."""

Expand Down
9 changes: 9 additions & 0 deletions tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ async def test_car_online(hass: HomeAssistant) -> None:
)
assert state.attributes.get("vin") == car_mock_data.VEHICLE["vin"]
assert state.attributes.get("id") == str(car_mock_data.VEHICLE["id"])
assert state.attributes.get("state") == car_mock_data.VEHICLE["state"]


async def test_car_asleep(hass: HomeAssistant) -> None:
"""Tests car asleep is getting the correct value."""
await setup_platform(hass, BINARY_SENSOR_DOMAIN)

state = hass.states.get("binary_sensor.my_model_s_asleep")
assert state.state == "off"


async def test_battery_charging(hass: HomeAssistant) -> None:
Expand Down

0 comments on commit 43deefb

Please sign in to comment.