StarLine integration (#27197)
* Device Tracker works * Device Tracker works * Binary Sensor * Sensor * Lock * Switch and service * New switches * Update interval options * WIP * Translation errors * Check online state * WIP * Move to aiohttp * Some checks * CI * CI * .coveragerc * Black * icon_for_signal_level test * update_interval renamed to scan_interval * async logic * Fix cookie read * Requirement starline * Reformat * Requirements updated * ConfigEntryNotReady * Requirement starline * Lint fix * Requirement starline * available status * Translations * Expiration to config * CI * Linter fix * Small renaming * Update slnet token * Starline version bump * Fix updates * Black * Small fix * Removed unused fields * CI * set_scan_interval service * deps updated * Horn switch * Starline lib updated * Starline lib updated * Black * Support multiple integrations * Review * async_will_remove_from_hass * Deps updated * Test config flow * Requirements * CI * Review * Review * Review * Review * Review * CI * pylint fix * Review * Support "mayak" devices * Icons removed * Removed options_flow * Removed options_flow test * Removed options_flow test
This commit is contained in:
parent
c21650473a
commit
a37260faa9
23 changed files with 1179 additions and 0 deletions
95
homeassistant/components/starline/sensor.py
Normal file
95
homeassistant/components/starline/sensor.py
Normal file
|
@ -0,0 +1,95 @@
|
|||
"""Reads vehicle status from StarLine API."""
|
||||
from homeassistant.components.sensor import DEVICE_CLASS_TEMPERATURE
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.icon import icon_for_battery_level, icon_for_signal_level
|
||||
from .account import StarlineAccount, StarlineDevice
|
||||
from .const import DOMAIN
|
||||
from .entity import StarlineEntity
|
||||
|
||||
SENSOR_TYPES = {
|
||||
"battery": ["Battery", None, "V", None],
|
||||
"balance": ["Balance", None, None, "mdi:cash-multiple"],
|
||||
"ctemp": ["Interior Temperature", DEVICE_CLASS_TEMPERATURE, None, None],
|
||||
"etemp": ["Engine Temperature", DEVICE_CLASS_TEMPERATURE, None, None],
|
||||
"gsm_lvl": ["GSM Signal", None, "%", None],
|
||||
}
|
||||
|
||||
|
||||
async def async_setup_entry(hass, entry, async_add_entities):
|
||||
"""Set up the StarLine sensors."""
|
||||
account: StarlineAccount = hass.data[DOMAIN][entry.entry_id]
|
||||
entities = []
|
||||
for device in account.api.devices.values():
|
||||
for key, value in SENSOR_TYPES.items():
|
||||
sensor = StarlineSensor(account, device, key, *value)
|
||||
if sensor.state is not None:
|
||||
entities.append(sensor)
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class StarlineSensor(StarlineEntity, Entity):
|
||||
"""Representation of a StarLine sensor."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
account: StarlineAccount,
|
||||
device: StarlineDevice,
|
||||
key: str,
|
||||
name: str,
|
||||
device_class: str,
|
||||
unit: str,
|
||||
icon: str,
|
||||
):
|
||||
"""Constructor."""
|
||||
super().__init__(account, device, key, name)
|
||||
self._device_class = device_class
|
||||
self._unit = unit
|
||||
self._icon = icon
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Icon to use in the frontend, if any."""
|
||||
if self._key == "battery":
|
||||
return icon_for_battery_level(
|
||||
battery_level=self._device.battery_level_percent,
|
||||
charging=self._device.car_state.get("ign", False),
|
||||
)
|
||||
if self._key == "gsm_lvl":
|
||||
return icon_for_signal_level(signal_level=self._device.gsm_level_percent)
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the sensor."""
|
||||
if self._key == "battery":
|
||||
return self._device.battery_level
|
||||
if self._key == "balance":
|
||||
return self._device.balance.get("value")
|
||||
if self._key == "ctemp":
|
||||
return self._device.temp_inner
|
||||
if self._key == "etemp":
|
||||
return self._device.temp_engine
|
||||
if self._key == "gsm_lvl":
|
||||
return self._device.gsm_level_percent
|
||||
return None
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Get the unit of measurement."""
|
||||
if self._key == "balance":
|
||||
return self._device.balance.get("currency") or "₽"
|
||||
return self._unit
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the class of the sensor."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes of the sensor."""
|
||||
if self._key == "balance":
|
||||
return self._account.balance_attrs(self._device)
|
||||
if self._key == "gsm_lvl":
|
||||
return self._account.gsm_attrs(self._device)
|
||||
return None
|
Loading…
Add table
Add a link
Reference in a new issue