Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Reads vehicle status from StarLine API."""
2 
3 from __future__ import annotations
4 
6  SensorDeviceClass,
7  SensorEntity,
8  SensorEntityDescription,
9  SensorStateClass,
10 )
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import (
13  PERCENTAGE,
14  EntityCategory,
15  UnitOfElectricPotential,
16  UnitOfLength,
17  UnitOfTemperature,
18  UnitOfVolume,
19 )
20 from homeassistant.core import HomeAssistant
21 from homeassistant.helpers.entity_platform import AddEntitiesCallback
22 from homeassistant.helpers.icon import icon_for_battery_level, icon_for_signal_level
23 
24 from .account import StarlineAccount, StarlineDevice
25 from .const import DOMAIN
26 from .entity import StarlineEntity
27 
28 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
30  key="battery",
31  translation_key="battery",
32  device_class=SensorDeviceClass.VOLTAGE,
33  native_unit_of_measurement=UnitOfElectricPotential.VOLT,
34  state_class=SensorStateClass.MEASUREMENT,
35  ),
37  key="balance",
38  translation_key="balance",
39  state_class=SensorStateClass.MEASUREMENT,
40  ),
42  key="ctemp",
43  translation_key="interior_temperature",
44  device_class=SensorDeviceClass.TEMPERATURE,
45  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
46  state_class=SensorStateClass.MEASUREMENT,
47  ),
49  key="etemp",
50  translation_key="engine_temperature",
51  device_class=SensorDeviceClass.TEMPERATURE,
52  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
53  state_class=SensorStateClass.MEASUREMENT,
54  ),
56  key="gsm_lvl",
57  translation_key="gsm_signal",
58  native_unit_of_measurement=PERCENTAGE,
59  state_class=SensorStateClass.MEASUREMENT,
60  ),
62  key="fuel",
63  translation_key="fuel",
64  state_class=SensorStateClass.MEASUREMENT,
65  ),
67  key="errors",
68  translation_key="errors",
69  native_unit_of_measurement="errors",
70  entity_category=EntityCategory.DIAGNOSTIC,
71  state_class=SensorStateClass.MEASUREMENT,
72  ),
74  key="mileage",
75  translation_key="mileage",
76  native_unit_of_measurement=UnitOfLength.KILOMETERS,
77  device_class=SensorDeviceClass.DISTANCE,
78  state_class=SensorStateClass.TOTAL_INCREASING,
79  ),
81  key="gps_count",
82  translation_key="gps_count",
83  native_unit_of_measurement="satellites",
84  state_class=SensorStateClass.MEASUREMENT,
85  ),
86 )
87 
88 
90  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
91 ) -> None:
92  """Set up the StarLine sensors."""
93  account: StarlineAccount = hass.data[DOMAIN][entry.entry_id]
94  entities = [
95  sensor
96  for device in account.api.devices.values()
97  for description in SENSOR_TYPES
98  if (sensor := StarlineSensor(account, device, description)).native_value
99  is not None
100  ]
101  async_add_entities(entities)
102 
103 
105  """Representation of a StarLine sensor."""
106 
107  def __init__(
108  self,
109  account: StarlineAccount,
110  device: StarlineDevice,
111  description: SensorEntityDescription,
112  ) -> None:
113  """Initialize StarLine sensor."""
114  super().__init__(account, device, description.key)
115  self.entity_descriptionentity_description = description
116 
117  @property
118  def icon(self):
119  """Icon to use in the frontend, if any."""
120  if self._key_key_key == "battery":
121  return icon_for_battery_level(
122  battery_level=self._device_device.battery_level_percent,
123  charging=self._device_device.car_state.get("ign", False),
124  )
125  if self._key_key_key == "gsm_lvl":
126  return icon_for_signal_level(signal_level=self._device_device.gsm_level_percent)
127  return self.entity_descriptionentity_description.icon
128 
129  @property
130  def native_value(self):
131  """Return the state of the sensor."""
132  if self._key_key_key == "battery":
133  return self._device_device.battery_level
134  if self._key_key_key == "balance":
135  return self._device_device.balance.get("value")
136  if self._key_key_key == "ctemp":
137  return self._device_device.temp_inner
138  if self._key_key_key == "etemp":
139  return self._device_device.temp_engine
140  if self._key_key_key == "gsm_lvl":
141  return self._device_device.gsm_level_percent
142  if self._key_key_key == "fuel" and self._device_device.fuel:
143  return self._device_device.fuel.get("val")
144  if self._key_key_key == "errors" and self._device_device.errors:
145  return self._device_device.errors.get("val")
146  if self._key_key_key == "mileage" and self._device_device.mileage:
147  return self._device_device.mileage.get("val")
148  if self._key_key_key == "gps_count" and self._device_device.position:
149  return self._device_device.position.get("sat_qty")
150  return None
151 
152  @property
154  """Get the unit of measurement."""
155  if self._key_key_key == "balance":
156  return self._device_device.balance.get("currency") or "₽"
157  if self._key_key_key == "fuel":
158  type_value = self._device_device.fuel.get("type")
159  if type_value == "percents":
160  return PERCENTAGE
161  if type_value == "litres":
162  return UnitOfVolume.LITERS
163  return self.entity_descriptionentity_description.native_unit_of_measurement
164 
165  @property
167  """Return the state attributes of the sensor."""
168  if self._key_key_key == "balance":
169  return self._account_account.balance_attrs(self._device_device)
170  if self._key_key_key == "gsm_lvl":
171  return self._account_account.gsm_attrs(self._device_device)
172  if self._key_key_key == "errors":
173  return self._account_account.errors_attrs(self._device_device)
174  return None
None __init__(self, StarlineAccount account, StarlineDevice device, SensorEntityDescription description)
Definition: sensor.py:112
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:91
str icon_for_battery_level(int|None battery_level=None, bool charging=False)
Definition: icon.py:169
str icon_for_signal_level(int|None signal_level=None)
Definition: icon.py:185