Add binary sensor to Tesla Fleet (#122225)
This commit is contained in:
parent
ecffae0b4f
commit
2b93de1348
6 changed files with 2029 additions and 1 deletions
|
@ -34,7 +34,7 @@ from .coordinator import (
|
|||
)
|
||||
from .models import TeslaFleetData, TeslaFleetEnergyData, TeslaFleetVehicleData
|
||||
|
||||
PLATFORMS: Final = [Platform.SENSOR]
|
||||
PLATFORMS: Final = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||
|
||||
type TeslaFleetConfigEntry = ConfigEntry[TeslaFleetData]
|
||||
|
||||
|
|
275
homeassistant/components/tesla_fleet/binary_sensor.py
Normal file
275
homeassistant/components/tesla_fleet/binary_sensor.py
Normal file
|
@ -0,0 +1,275 @@
|
|||
"""Binary Sensor platform for Tesla Fleet integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from itertools import chain
|
||||
from typing import cast
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from . import TeslaFleetConfigEntry
|
||||
from .const import TeslaFleetState
|
||||
from .entity import (
|
||||
TeslaFleetEnergyInfoEntity,
|
||||
TeslaFleetEnergyLiveEntity,
|
||||
TeslaFleetVehicleEntity,
|
||||
)
|
||||
from .models import TeslaFleetEnergyData, TeslaFleetVehicleData
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class TeslaFleetBinarySensorEntityDescription(BinarySensorEntityDescription):
|
||||
"""Describes Tesla Fleet binary sensor entity."""
|
||||
|
||||
is_on: Callable[[StateType], bool] = bool
|
||||
|
||||
|
||||
VEHICLE_DESCRIPTIONS: tuple[TeslaFleetBinarySensorEntityDescription, ...] = (
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="state",
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
is_on=lambda x: x == TeslaFleetState.ONLINE,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="charge_state_battery_heater_on",
|
||||
device_class=BinarySensorDeviceClass.HEAT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="charge_state_charger_phases",
|
||||
is_on=lambda x: cast(int, x) > 1,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="charge_state_preconditioning_enabled",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="climate_state_is_preconditioning",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="charge_state_scheduled_charging_pending",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="charge_state_trip_charging",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="charge_state_conn_charge_cable",
|
||||
is_on=lambda x: x != "<invalid>",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
device_class=BinarySensorDeviceClass.CONNECTIVITY,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="climate_state_cabin_overheat_protection_actively_cooling",
|
||||
device_class=BinarySensorDeviceClass.HEAT,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_dashcam_state",
|
||||
device_class=BinarySensorDeviceClass.RUNNING,
|
||||
is_on=lambda x: x == "Recording",
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_is_user_present",
|
||||
device_class=BinarySensorDeviceClass.PRESENCE,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_tpms_soft_warning_fl",
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_tpms_soft_warning_fr",
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_tpms_soft_warning_rl",
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_tpms_soft_warning_rr",
|
||||
device_class=BinarySensorDeviceClass.PROBLEM,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
entity_registry_enabled_default=False,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_fd_window",
|
||||
device_class=BinarySensorDeviceClass.WINDOW,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_fp_window",
|
||||
device_class=BinarySensorDeviceClass.WINDOW,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_rd_window",
|
||||
device_class=BinarySensorDeviceClass.WINDOW,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_rp_window",
|
||||
device_class=BinarySensorDeviceClass.WINDOW,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_df",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_dr",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_pf",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
TeslaFleetBinarySensorEntityDescription(
|
||||
key="vehicle_state_pr",
|
||||
device_class=BinarySensorDeviceClass.DOOR,
|
||||
entity_category=EntityCategory.DIAGNOSTIC,
|
||||
),
|
||||
)
|
||||
|
||||
ENERGY_LIVE_DESCRIPTIONS: tuple[BinarySensorEntityDescription, ...] = (
|
||||
BinarySensorEntityDescription(key="backup_capable"),
|
||||
BinarySensorEntityDescription(key="grid_services_active"),
|
||||
)
|
||||
|
||||
|
||||
ENERGY_INFO_DESCRIPTIONS: tuple[BinarySensorEntityDescription, ...] = (
|
||||
BinarySensorEntityDescription(
|
||||
key="components_grid_services_enabled",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: TeslaFleetConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Tesla Fleet binary sensor platform from a config entry."""
|
||||
|
||||
async_add_entities(
|
||||
chain(
|
||||
( # Vehicles
|
||||
TeslaFleetVehicleBinarySensorEntity(vehicle, description)
|
||||
for vehicle in entry.runtime_data.vehicles
|
||||
for description in VEHICLE_DESCRIPTIONS
|
||||
),
|
||||
( # Energy Site Live
|
||||
TeslaFleetEnergyLiveBinarySensorEntity(energysite, description)
|
||||
for energysite in entry.runtime_data.energysites
|
||||
for description in ENERGY_LIVE_DESCRIPTIONS
|
||||
if energysite.info_coordinator.data.get("components_battery")
|
||||
),
|
||||
( # Energy Site Info
|
||||
TeslaFleetEnergyInfoBinarySensorEntity(energysite, description)
|
||||
for energysite in entry.runtime_data.energysites
|
||||
for description in ENERGY_INFO_DESCRIPTIONS
|
||||
if energysite.info_coordinator.data.get("components_battery")
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class TeslaFleetVehicleBinarySensorEntity(TeslaFleetVehicleEntity, BinarySensorEntity):
|
||||
"""Base class for Tesla Fleet vehicle binary sensors."""
|
||||
|
||||
entity_description: TeslaFleetBinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslaFleetVehicleData,
|
||||
description: TeslaFleetBinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
self.entity_description = description
|
||||
super().__init__(data, description.key)
|
||||
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the binary sensor."""
|
||||
|
||||
if self.coordinator.updated_once:
|
||||
if self._value is None:
|
||||
self._attr_available = False
|
||||
self._attr_is_on = None
|
||||
else:
|
||||
self._attr_available = True
|
||||
self._attr_is_on = self.entity_description.is_on(self._value)
|
||||
else:
|
||||
self._attr_is_on = None
|
||||
|
||||
|
||||
class TeslaFleetEnergyLiveBinarySensorEntity(
|
||||
TeslaFleetEnergyLiveEntity, BinarySensorEntity
|
||||
):
|
||||
"""Base class for Tesla Fleet energy live binary sensors."""
|
||||
|
||||
entity_description: BinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslaFleetEnergyData,
|
||||
description: BinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
self.entity_description = description
|
||||
super().__init__(data, description.key)
|
||||
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the binary sensor."""
|
||||
self._attr_is_on = self._value
|
||||
|
||||
|
||||
class TeslaFleetEnergyInfoBinarySensorEntity(
|
||||
TeslaFleetEnergyInfoEntity, BinarySensorEntity
|
||||
):
|
||||
"""Base class for Tesla Fleet energy info binary sensors."""
|
||||
|
||||
entity_description: BinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
data: TeslaFleetEnergyData,
|
||||
description: BinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
self.entity_description = description
|
||||
super().__init__(data, description.key)
|
||||
|
||||
def _async_update_attrs(self) -> None:
|
||||
"""Update the attributes of the binary sensor."""
|
||||
self._attr_is_on = self._value
|
|
@ -1,5 +1,43 @@
|
|||
{
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"climate_state_is_preconditioning": {
|
||||
"state": {
|
||||
"off": "mdi:hvac-off",
|
||||
"on": "mdi:hvac"
|
||||
}
|
||||
},
|
||||
"vehicle_state_is_user_present": {
|
||||
"state": {
|
||||
"off": "mdi:account-remove-outline",
|
||||
"on": "mdi:account"
|
||||
}
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_fl": {
|
||||
"state": {
|
||||
"off": "mdi:tire",
|
||||
"on": "mdi:car-tire-alert"
|
||||
}
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_fr": {
|
||||
"state": {
|
||||
"off": "mdi:tire",
|
||||
"on": "mdi:car-tire-alert"
|
||||
}
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_rl": {
|
||||
"state": {
|
||||
"off": "mdi:tire",
|
||||
"on": "mdi:car-tire-alert"
|
||||
}
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_rr": {
|
||||
"state": {
|
||||
"off": "mdi:tire",
|
||||
"on": "mdi:car-tire-alert"
|
||||
}
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"battery_power": {
|
||||
"default": "mdi:home-battery"
|
||||
|
|
|
@ -27,6 +27,86 @@
|
|||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"backup_capable": {
|
||||
"name": "Backup capable"
|
||||
},
|
||||
"charge_state_battery_heater_on": {
|
||||
"name": "Battery heater"
|
||||
},
|
||||
"charge_state_charger_phases": {
|
||||
"name": "Charger has multiple phases"
|
||||
},
|
||||
"charge_state_conn_charge_cable": {
|
||||
"name": "Charge cable"
|
||||
},
|
||||
"charge_state_preconditioning_enabled": {
|
||||
"name": "Preconditioning enabled"
|
||||
},
|
||||
"charge_state_scheduled_charging_pending": {
|
||||
"name": "Scheduled charging pending"
|
||||
},
|
||||
"charge_state_trip_charging": {
|
||||
"name": "Trip charging"
|
||||
},
|
||||
"climate_state_cabin_overheat_protection_actively_cooling": {
|
||||
"name": "Cabin overheat protection actively cooling"
|
||||
},
|
||||
"climate_state_is_preconditioning": {
|
||||
"name": "Preconditioning"
|
||||
},
|
||||
"components_grid_services_enabled": {
|
||||
"name": "Grid services enabled"
|
||||
},
|
||||
"grid_services_active": {
|
||||
"name": "Grid services active"
|
||||
},
|
||||
"state": {
|
||||
"name": "Status"
|
||||
},
|
||||
"vehicle_state_dashcam_state": {
|
||||
"name": "Dashcam"
|
||||
},
|
||||
"vehicle_state_df": {
|
||||
"name": "Front driver door"
|
||||
},
|
||||
"vehicle_state_dr": {
|
||||
"name": "Rear driver door"
|
||||
},
|
||||
"vehicle_state_fd_window": {
|
||||
"name": "Front driver window"
|
||||
},
|
||||
"vehicle_state_fp_window": {
|
||||
"name": "Front passenger window"
|
||||
},
|
||||
"vehicle_state_is_user_present": {
|
||||
"name": "User present"
|
||||
},
|
||||
"vehicle_state_pf": {
|
||||
"name": "Front passenger door"
|
||||
},
|
||||
"vehicle_state_pr": {
|
||||
"name": "Rear passenger door"
|
||||
},
|
||||
"vehicle_state_rd_window": {
|
||||
"name": "Rear driver window"
|
||||
},
|
||||
"vehicle_state_rp_window": {
|
||||
"name": "Rear passenger window"
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_fl": {
|
||||
"name": "Tire pressure warning front left"
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_fr": {
|
||||
"name": "Tire pressure warning front right"
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_rl": {
|
||||
"name": "Tire pressure warning rear left"
|
||||
},
|
||||
"vehicle_state_tpms_soft_warning_rr": {
|
||||
"name": "Tire pressure warning rear right"
|
||||
}
|
||||
},
|
||||
"sensor": {
|
||||
"battery_power": {
|
||||
"name": "Battery power"
|
||||
|
|
1571
tests/components/tesla_fleet/snapshots/test_binary_sensors.ambr
Normal file
1571
tests/components/tesla_fleet/snapshots/test_binary_sensors.ambr
Normal file
File diff suppressed because it is too large
Load diff
64
tests/components/tesla_fleet/test_binary_sensors.py
Normal file
64
tests/components/tesla_fleet/test_binary_sensors.py
Normal file
|
@ -0,0 +1,64 @@
|
|||
"""Test the Tesla Fleet binary sensor platform."""
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
import pytest
|
||||
from syrupy import SnapshotAssertion
|
||||
from tesla_fleet_api.exceptions import VehicleOffline
|
||||
|
||||
from homeassistant.components.tesla_fleet.coordinator import VEHICLE_INTERVAL
|
||||
from homeassistant.const import STATE_UNKNOWN, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_registry as er
|
||||
|
||||
from . import assert_entities, assert_entities_alt, setup_platform
|
||||
from .const import VEHICLE_DATA_ALT
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_binary_sensor(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
normal_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Tests that the binary sensor entities are correct."""
|
||||
|
||||
await setup_platform(hass, normal_config_entry, [Platform.BINARY_SENSOR])
|
||||
assert_entities(hass, normal_config_entry.entry_id, entity_registry, snapshot)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
|
||||
async def test_binary_sensor_refresh(
|
||||
hass: HomeAssistant,
|
||||
snapshot: SnapshotAssertion,
|
||||
entity_registry: er.EntityRegistry,
|
||||
mock_vehicle_data,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
normal_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Tests that the binary sensor entities are correct."""
|
||||
|
||||
await setup_platform(hass, normal_config_entry, [Platform.BINARY_SENSOR])
|
||||
|
||||
# Refresh
|
||||
mock_vehicle_data.return_value = VEHICLE_DATA_ALT
|
||||
freezer.tick(VEHICLE_INTERVAL)
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert_entities_alt(hass, normal_config_entry.entry_id, entity_registry, snapshot)
|
||||
|
||||
|
||||
async def test_binary_sensor_offline(
|
||||
hass: HomeAssistant,
|
||||
mock_vehicle_data,
|
||||
normal_config_entry: MockConfigEntry,
|
||||
) -> None:
|
||||
"""Tests that the binary sensor entities are correct when offline."""
|
||||
|
||||
mock_vehicle_data.side_effect = VehicleOffline
|
||||
await setup_platform(hass, normal_config_entry, [Platform.BINARY_SENSOR])
|
||||
state = hass.states.get("binary_sensor.test_status")
|
||||
assert state.state == STATE_UNKNOWN
|
Loading…
Add table
Reference in a new issue