From fdefa52ff9a520848a50900b3b3d0c44d079e643 Mon Sep 17 00:00:00 2001 From: Tianwei Dong Date: Sat, 17 Jun 2023 07:01:20 +0100 Subject: [PATCH] feat: add car windows binary_sensors (#629) * [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 --- .../tesla_custom/binary_sensor.py | 43 +++++++++++++++++++ tests/test_binary_sensor.py | 15 +++++++ 2 files changed, 58 insertions(+) diff --git a/custom_components/tesla_custom/binary_sensor.py b/custom_components/tesla_custom/binary_sensor.py index 741feca8..6998afbe 100644 --- a/custom_components/tesla_custom/binary_sensor.py +++ b/custom_components/tesla_custom/binary_sensor.py @@ -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)) @@ -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.""" diff --git a/tests/test_binary_sensor.py b/tests/test_binary_sensor.py index eab75c64..f40165ca 100644 --- a/tests/test_binary_sensor.py +++ b/tests/test_binary_sensor.py @@ -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)