-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Binary Sensor platform to Tessie (#105421)
- Loading branch information
Showing
5 changed files
with
237 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
"""Binary Sensor platform for Tessie integration.""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Callable | ||
from dataclasses import dataclass | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorDeviceClass, | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import EntityCategory | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from .const import DOMAIN | ||
from .coordinator import TessieDataUpdateCoordinator | ||
from .entity import TessieEntity | ||
|
||
|
||
@dataclass | ||
class TessieBinarySensorEntityDescription(BinarySensorEntityDescription): | ||
"""Describes Tessie binary sensor entity.""" | ||
|
||
is_on: Callable[..., bool] = lambda x: x | ||
|
||
|
||
DESCRIPTIONS: tuple[TessieBinarySensorEntityDescription, ...] = ( | ||
TessieBinarySensorEntityDescription( | ||
key="charge_state_battery_heater_on", | ||
device_class=BinarySensorDeviceClass.HEAT, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="charge_state_charging_state", | ||
device_class=BinarySensorDeviceClass.BATTERY_CHARGING, | ||
is_on=lambda x: x == "Charging", | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="charge_state_preconditioning_enabled", | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="charge_state_scheduled_charging_pending", | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="charge_state_trip_charging", | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="climate_state_auto_seat_climate_left", | ||
device_class=BinarySensorDeviceClass.HEAT, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="climate_state_auto_seat_climate_right", | ||
device_class=BinarySensorDeviceClass.HEAT, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="climate_state_auto_steering_wheel_heat", | ||
device_class=BinarySensorDeviceClass.HEAT, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="climate_state_cabin_overheat_protection", | ||
device_class=BinarySensorDeviceClass.RUNNING, | ||
is_on=lambda x: x == "On", | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="climate_state_cabin_overheat_protection_actively_cooling", | ||
device_class=BinarySensorDeviceClass.HEAT, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="vehicle_state_dashcam_state", | ||
device_class=BinarySensorDeviceClass.RUNNING, | ||
is_on=lambda x: x == "Recording", | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="vehicle_state_is_user_present", | ||
device_class=BinarySensorDeviceClass.PRESENCE, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="vehicle_state_tpms_soft_warning_fl", | ||
device_class=BinarySensorDeviceClass.PROBLEM, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="vehicle_state_tpms_soft_warning_fr", | ||
device_class=BinarySensorDeviceClass.PROBLEM, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="vehicle_state_tpms_soft_warning_rl", | ||
device_class=BinarySensorDeviceClass.PROBLEM, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
TessieBinarySensorEntityDescription( | ||
key="vehicle_state_tpms_soft_warning_rr", | ||
device_class=BinarySensorDeviceClass.PROBLEM, | ||
entity_category=EntityCategory.DIAGNOSTIC, | ||
), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up the Tessie binary sensor platform from a config entry.""" | ||
coordinators = hass.data[DOMAIN][entry.entry_id] | ||
|
||
async_add_entities( | ||
TessieBinarySensorEntity(coordinator, description) | ||
for coordinator in coordinators | ||
for description in DESCRIPTIONS | ||
if description.key in coordinator.data | ||
) | ||
|
||
|
||
class TessieBinarySensorEntity(TessieEntity, BinarySensorEntity): | ||
"""Base class for Tessie binary sensors.""" | ||
|
||
entity_description: TessieBinarySensorEntityDescription | ||
|
||
def __init__( | ||
self, | ||
coordinator: TessieDataUpdateCoordinator, | ||
description: TessieBinarySensorEntityDescription, | ||
) -> None: | ||
"""Initialize the sensor.""" | ||
super().__init__(coordinator, description.key) | ||
self.entity_description = description | ||
|
||
@property | ||
def is_on(self) -> bool: | ||
"""Return the state of the binary sensor.""" | ||
return self.entity_description.is_on(self._value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Test the Tessie binary sensor platform.""" | ||
|
||
from homeassistant.components.tessie.binary_sensor import DESCRIPTIONS | ||
from homeassistant.const import STATE_OFF, STATE_ON | ||
from homeassistant.core import HomeAssistant | ||
|
||
from .common import TEST_VEHICLE_STATE_ONLINE, setup_platform | ||
|
||
OFFON = [STATE_OFF, STATE_ON] | ||
|
||
|
||
async def test_binary_sensors(hass: HomeAssistant) -> None: | ||
"""Tests that the sensors are correct.""" | ||
|
||
assert len(hass.states.async_all("binary_sensor")) == 0 | ||
|
||
await setup_platform(hass) | ||
|
||
assert len(hass.states.async_all("binary_sensor")) == len(DESCRIPTIONS) | ||
|
||
state = hass.states.get("binary_sensor.test_battery_heater").state | ||
is_on = state == STATE_ON | ||
assert is_on == TEST_VEHICLE_STATE_ONLINE["charge_state"]["battery_heater_on"] | ||
|
||
state = hass.states.get("binary_sensor.test_charging").state | ||
is_on = state == STATE_ON | ||
assert is_on == ( | ||
TEST_VEHICLE_STATE_ONLINE["charge_state"]["charging_state"] == "Charging" | ||
) | ||
|
||
state = hass.states.get("binary_sensor.test_auto_seat_climate_left").state | ||
is_on = state == STATE_ON | ||
assert is_on == TEST_VEHICLE_STATE_ONLINE["climate_state"]["auto_seat_climate_left"] |