"""Reads vehicle status from StarLine API.""" from __future__ import annotations from dataclasses import dataclass from homeassistant.components.binary_sensor import ( BinarySensorDeviceClass, BinarySensorEntity, BinarySensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from .account import StarlineAccount, StarlineDevice from .const import DOMAIN from .entity import StarlineEntity @dataclass class StarlineRequiredKeysMixin: """Mixin for required keys.""" name_: str @dataclass class StarlineBinarySensorEntityDescription( BinarySensorEntityDescription, StarlineRequiredKeysMixin ): """Describes Starline binary_sensor entity.""" BINARY_SENSOR_TYPES: tuple[StarlineBinarySensorEntityDescription, ...] = ( StarlineBinarySensorEntityDescription( key="hbrake", name_="Hand Brake", device_class=BinarySensorDeviceClass.POWER, ), StarlineBinarySensorEntityDescription( key="hood", name_="Hood", device_class=BinarySensorDeviceClass.DOOR, ), StarlineBinarySensorEntityDescription( key="trunk", name_="Trunk", device_class=BinarySensorDeviceClass.DOOR, ), StarlineBinarySensorEntityDescription( key="alarm", name_="Alarm", device_class=BinarySensorDeviceClass.PROBLEM, ), StarlineBinarySensorEntityDescription( key="door", name_="Doors", device_class=BinarySensorDeviceClass.LOCK, ), ) async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: """Set up the StarLine sensors.""" account: StarlineAccount = hass.data[DOMAIN][entry.entry_id] entities = [ sensor for device in account.api.devices.values() for description in BINARY_SENSOR_TYPES if description.key in device.car_state if (sensor := StarlineSensor(account, device, description)).is_on is not None ] async_add_entities(entities) class StarlineSensor(StarlineEntity, BinarySensorEntity): """Representation of a StarLine binary sensor.""" entity_description: StarlineBinarySensorEntityDescription def __init__( self, account: StarlineAccount, device: StarlineDevice, description: StarlineBinarySensorEntityDescription, ) -> None: """Initialize sensor.""" super().__init__(account, device, description.key, description.name_) self.entity_description = description @property def is_on(self): """Return the state of the binary sensor.""" return self._device.car_state.get(self._key)