Skip to content

Commit

Permalink
feat: add car windows binary_sensors (#629)
Browse files Browse the repository at this point in the history
* [Add] extra attributes for windows

* style: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update test_binary_sensor.py

* style: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update car.py

* Update custom_components/tesla_custom/binary_sensor.py

* style: auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update custom_components/tesla_custom/binary_sensor.py

* Update tests/mock_data/car.py

* Update tests/test_binary_sensor.py

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Alan Tse <alandtse@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 17, 2023
1 parent db1d6b8 commit fdefa52
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
43 changes: 43 additions & 0 deletions custom_components/tesla_custom/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
entities.append(TeslaCarChargerConnection(hass, car, coordinator))
entities.append(TeslaCarCharging(hass, car, coordinator))
entities.append(TeslaCarDoors(hass, car, coordinator))
entities.append(TeslaCarWindows(hass, car, coordinator))
entities.append(TeslaCarScheduledCharging(hass, car, coordinator))
entities.append(TeslaCarScheduledDeparture(hass, car, coordinator))
entities.append(TeslaCarUserPresent(hass, car, coordinator))
Expand Down Expand Up @@ -255,6 +256,48 @@ def _open_or_closed(self, door):
return "Closed"


class TeslaCarWindows(TeslaCarEntity, BinarySensorEntity):
"""Representation of a Tesla window door sensor."""

def __init__(
self,
hass: HomeAssistant,
car: TeslaCar,
coordinator: TeslaDataUpdateCoordinator,
) -> None:
"""Initialize car windows entity."""
super().__init__(hass, car, coordinator)
self.type = "windows"
self._attr_device_class = BinarySensorDeviceClass.WINDOW
self._attr_icon = "mdi:car-door"

@property
def is_on(self):
"""Return True if a car window is open."""
return (
self._car.window_fd
or self._car.window_fp
or self._car.window_rd
or self._car.window_rp
)

@property
def extra_state_attributes(self):
"""Return device state attributes."""
return {
"Driver Front": self._open_or_closed(self._car.window_fd),
"Driver Rear": self._open_or_closed(self._car.window_rd),
"Passenger Front": self._open_or_closed(self._car.window_fp),
"Passenger Rear": self._open_or_closed(self._car.window_rp),
}

def _open_or_closed(self, window):
"""Return string of 'Open' or 'Closed' when passed a window integer state."""
if window:
return "Open"
return "Closed"


class TeslaCarScheduledCharging(TeslaCarEntity, BinarySensorEntity):
"""Representation of a Tesla car scheduled charging binary sensor."""

Expand Down
15 changes: 15 additions & 0 deletions tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,21 @@ async def test_car_doors(hass: HomeAssistant) -> None:
assert state.attributes.get("Passenger Rear") == "Closed"


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

state = hass.states.get("binary_sensor.my_model_s_windows")
assert state.state == STATE_OFF

assert state.attributes.get(ATTR_DEVICE_CLASS) == BinarySensorDeviceClass.WINDOW

assert state.attributes.get("Driver Front") == "Closed"
assert state.attributes.get("Driver Rear") == "Closed"
assert state.attributes.get("Passenger Front") == "Closed"
assert state.attributes.get("Passenger Rear") == "Closed"


async def test_car_scheduled_charging(hass: HomeAssistant) -> None:
"""Tests scheduled charging is getting the correct value."""
await setup_platform(hass, BINARY_SENSOR_DOMAIN)
Expand Down

0 comments on commit fdefa52

Please sign in to comment.