-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Binary Sensor platform to Tessie #105421
Changes from 7 commits
bbc1234
c5a917b
ee8433b
3ad52d9
15cbfcf
f7e1824
50561e3
0d2cdad
72e826a
4e51c1a
5c6d7d4
2f520cd
06fd0a9
a038f66
e35fcdf
25ea811
b8241da
d33bf27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, ...] = ( | ||
Bre77 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.native_value) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OFFON isn't used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, that was an idea I gave up on. I'll raise a PR to remove it. |
||
|
||
|
||
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" | ||
Check failure on line 27 in tests/components/tessie/test_binary_sensors.py GitHub Actions / Run tests Python 3.11 (tessie)
|
||
|
||
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"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a followup these should be changed due to a recent change https://developers.home-assistant.io/blog/2023/12/11/entity-description-changes/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will come up a few times in the other platforms, but ill look at addressing them all once I fully understand the repercussions.