1+ """Support for Kamereon cars."""
2+ import logging
3+
4+ from homeassistant .components .binary_sensor import DEVICE_CLASSES , BinarySensorDevice
5+ from homeassistant .const import STATE_UNKNOWN
6+
7+ from . import KamereonEntity
8+ from .kamereon import ChargingStatus , Door , LockStatus , PluggedStatus
9+
10+ _LOGGER = logging .getLogger (__name__ )
11+
12+
13+ async def async_setup_platform (hass , config , async_add_entities , vehicle = None ):
14+ """Set up the Kamereon sensors."""
15+ if vehicle is None :
16+ return
17+ async_add_entities ([
18+ ChargingStatusEntity (vehicle ),
19+ PluggedStatusEntity (vehicle ),
20+ FuelLowWarningEntity (vehicle ),
21+ DoorEntity (vehicle , Door .FRONT_LEFT ),
22+ DoorEntity (vehicle , Door .FRONT_RIGHT ),
23+ DoorEntity (vehicle , Door .REAR_LEFT ),
24+ DoorEntity (vehicle , Door .REAR_RIGHT ),
25+ DoorEntity (vehicle , Door .HATCH ),
26+ ])
27+
28+
29+ class ChargingStatusEntity (KamereonEntity , BinarySensorDevice ):
30+ """Representation of charging status."""
31+
32+ @property
33+ def _entity_name (self ):
34+ return 'charging'
35+
36+ @property
37+ def icon (self ):
38+ """Return the icon."""
39+ return 'mdi:{}' .format ('battery-charging' if self .is_on else 'battery-off' )
40+
41+ @property
42+ def is_on (self ):
43+ """Return True if the binary sensor is on."""
44+ if self .vehicle .charging is None :
45+ return STATE_UNKNOWN
46+ return self .vehicle .charging is ChargingStatus .CHARGING
47+
48+ @property
49+ def device_class (self ):
50+ """Return the class of this sensor."""
51+ return 'power'
52+
53+ @property
54+ def device_state_attributes (self ):
55+ a = KamereonEntity .device_state_attributes .fget (self )
56+ a .update ({
57+ 'charging_speed' : self .vehicle .charging_speed .value ,
58+ 'last_updated' : self .vehicle .battery_status_last_updated ,
59+ })
60+ return a
61+
62+
63+ class PluggedStatusEntity (KamereonEntity , BinarySensorDevice ):
64+ """Representation of plugged status."""
65+
66+ @property
67+ def _entity_name (self ):
68+ return 'plugged_in'
69+
70+ @property
71+ def icon (self ):
72+ """Return the icon."""
73+ return 'mdi:{}' .format ('power-plug' if self .is_on else 'power-plug-off' )
74+
75+ @property
76+ def is_on (self ):
77+ """Return True if the binary sensor is on."""
78+ if self .vehicle .plugged_in is None :
79+ return STATE_UNKNOWN
80+ return self .vehicle .plugged_in is PluggedStatus .PLUGGED
81+
82+ @property
83+ def device_class (self ):
84+ """Return the class of this sensor."""
85+ return 'plug'
86+
87+ @property
88+ def device_state_attributes (self ):
89+ a = KamereonEntity .device_state_attributes .fget (self )
90+ a .update ({
91+ 'plugged_in_time' : self .vehicle .plugged_in_time ,
92+ 'unplugged_time' : self .vehicle .unplugged_time ,
93+ 'last_updated' : self .vehicle .battery_status_last_updated ,
94+ })
95+ return a
96+
97+
98+ class FuelLowWarningEntity (KamereonEntity , BinarySensorDevice ):
99+ """Representation of fuel low warning status."""
100+
101+ @property
102+ def _entity_name (self ):
103+ return 'fuel_low'
104+
105+ @property
106+ def icon (self ):
107+ """Return the icon."""
108+ return 'mdi:fuel'
109+
110+ @property
111+ def is_on (self ):
112+ """Return True if the binary sensor is on."""
113+ if self .vehicle .fuel_low_warning is None :
114+ return STATE_UNKNOWN
115+ return self .vehicle .fuel_low_warning
116+
117+ @property
118+ def device_class (self ):
119+ """Return the class of this sensor."""
120+ return 'safety'
121+
122+
123+ class DoorEntity (KamereonEntity , BinarySensorDevice ):
124+ """Representation of a door (or hatch)."""
125+
126+ def __init__ (self , vehicle , door ):
127+ KamereonEntity .__init__ (self , vehicle )
128+ self .door = door
129+
130+ @property
131+ def icon (self ):
132+ """Return the icon."""
133+ return 'mdi:car-door'
134+
135+ @property
136+ def _entity_name (self ):
137+ return '{}_door' .format (self .door .value )
138+
139+ @property
140+ def is_on (self ):
141+ """Return True if the binary sensor is open."""
142+ if self .door not in self .vehicle .door_status or self .vehicle .door_status [self .door ] is None :
143+ return STATE_UNKNOWN
144+ return self .vehicle .door_status [self .door ] == LockStatus .OPEN
145+
146+ @property
147+ def device_class (self ):
148+ """Return the class of this sensor."""
149+ return 'door'
0 commit comments