-
Notifications
You must be signed in to change notification settings - Fork 4
/
sensor.py
119 lines (99 loc) · 3.63 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
SensorStateClass,
SensorDeviceClass,
)
from homeassistant.helpers.typing import StateType
from . import TronityEntity
from .const import DOMAIN, CONF_DATA_COORDINATOR
SENSOR_ENTITIES = [
SensorEntityDescription(
key="odometer",
icon="mdi:speedometer",
device_class=SensorDeviceClass.DISTANCE,
native_unit_of_measurement="km",
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="range",
icon="mdi:ev-station",
device_class=SensorDeviceClass.DISTANCE,
native_unit_of_measurement="km",
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="level",
device_class=SensorDeviceClass.BATTERY,
native_unit_of_measurement="%",
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="charging",
icon="mdi:battery-charging",
),
SensorEntityDescription(
key="plugged",
icon="mdi:power-plug",
device_class=SensorDeviceClass.ENUM,
options=[False, True],
),
SensorEntityDescription(
key="chargerPower",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement="kW",
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="chargeRemainingTime",
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement="s",
state_class=SensorStateClass.MEASUREMENT,
),
]
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the sensor platform."""
coordinator = hass.data[DOMAIN][config_entry.entry_id][CONF_DATA_COORDINATOR]
entities: list[SensorEntity] = [DisplayName(coordinator, config_entry)]
for description in SENSOR_ENTITIES:
entities.append(TronitySensorEntity(coordinator, description, config_entry))
async_add_entities(entities)
class TronitySensorEntity(SensorEntity, TronityEntity):
"""Representation of a Tronity vehicle sensor."""
entity_description: SensorEntityDescription
def __init__(self, coordinator, description, config_entry: ConfigEntry) -> None:
"""Initialize Tronity sensor."""
super().__init__(coordinator, config_entry)
self.entity_description = description
self._attr_unique_id = f"{self.vehicle_id}_{description.key}"
@property
def name(self) -> str:
"""Return name of the sensor."""
return f"tronity.{self.display_name}.{self.entity_description.key}"
@property
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self.data[self.entity_description.key]
class DisplayName(SensorEntity, TronityEntity):
"""Fetch display name."""
entity_description: SensorEntityDescription
def __init__(self, coordinator, config_entry: ConfigEntry) -> None:
"""Initialize Tronity sensor."""
super().__init__(coordinator, config_entry)
self._attr_unique_id = f"{self.vehicle_id}_display_name"
self._attr_icon = "mdi:car"
@property
def name(self) -> str:
"""Return name of the sensor."""
return "tronity.display_name"
@property
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self.display_name