forked from kukulich/home-assistant-jablotron100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
45 lines (30 loc) · 1.42 KB
/
sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from homeassistant import config_entries, core
from homeassistant.components.sensor import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_SIGNAL_STRENGTH,
SensorEntity,
STATE_CLASS_MEASUREMENT,
)
from homeassistant.const import PERCENTAGE
from .const import (
DATA_JABLOTRON,
DOMAIN,
)
from .jablotron import JablotronEntity
async def async_setup_entry(hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry, async_add_entities) -> None:
jablotron = hass.data[DOMAIN][config_entry.entry_id][DATA_JABLOTRON]
async_add_entities((JablotronSignalStrengthEntity(jablotron, control) for control in jablotron.device_signal_strength_sensors()))
async_add_entities((JablotronBatteryLevelEntity(jablotron, control) for control in jablotron.device_battery_level_sensors()))
gsm_signal_strength_sensor = jablotron.gsm_signal_strength_sensor()
if gsm_signal_strength_sensor is not None:
async_add_entities([JablotronSignalStrengthEntity(jablotron, gsm_signal_strength_sensor)])
class JablotronSensor(JablotronEntity, SensorEntity):
_attr_native_unit_of_measurement = PERCENTAGE
_attr_state_class = STATE_CLASS_MEASUREMENT
def _update_attributes(self) -> None:
super()._update_attributes()
self._attr_native_value = self._get_state()
class JablotronSignalStrengthEntity(JablotronSensor):
_attr_device_class = DEVICE_CLASS_SIGNAL_STRENGTH
class JablotronBatteryLevelEntity(JablotronSensor):
_attr_device_class = DEVICE_CLASS_BATTERY