hass-core/homeassistant/components/starline/binary_sensor.py
Nikolay Vasilchuk a37260faa9 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
2019-11-26 11:17:11 -08:00

58 lines
1.7 KiB
Python

"""Reads vehicle status from StarLine API."""
from homeassistant.components.binary_sensor import (
BinarySensorDevice,
DEVICE_CLASS_DOOR,
DEVICE_CLASS_LOCK,
DEVICE_CLASS_PROBLEM,
DEVICE_CLASS_POWER,
)
from .account import StarlineAccount, StarlineDevice
from .const import DOMAIN
from .entity import StarlineEntity
SENSOR_TYPES = {
"hbrake": ["Hand Brake", DEVICE_CLASS_POWER],
"hood": ["Hood", DEVICE_CLASS_DOOR],
"trunk": ["Trunk", DEVICE_CLASS_DOOR],
"alarm": ["Alarm", DEVICE_CLASS_PROBLEM],
"door": ["Doors", DEVICE_CLASS_LOCK],
}
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():
if key in device.car_state:
sensor = StarlineSensor(account, device, key, *value)
if sensor.is_on is not None:
entities.append(sensor)
async_add_entities(entities)
class StarlineSensor(StarlineEntity, BinarySensorDevice):
"""Representation of a StarLine binary sensor."""
def __init__(
self,
account: StarlineAccount,
device: StarlineDevice,
key: str,
name: str,
device_class: str,
):
"""Constructor."""
super().__init__(account, device, key, name)
self._device_class = device_class
@property
def device_class(self):
"""Return the class of the binary sensor."""
return self._device_class
@property
def is_on(self):
"""Return the state of the binary sensor."""
return self._device.car_state.get(self._key)