-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsensor.py
More file actions
69 lines (56 loc) · 2.47 KB
/
sensor.py
File metadata and controls
69 lines (56 loc) · 2.47 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Sensor platform for BWT integration."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN, MANUFACTURER, SENSOR_TYPES, CONF_SERIAL_NUMBER, CONF_DEVICE_NAME, DEFAULT_DEVICE_NAME
from .coordinator import BWTDataUpdateCoordinator
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up BWT sensor based on a config entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
entities = []
for sensor_type in SENSOR_TYPES:
entities.append(BWTSensor(coordinator, entry, sensor_type))
async_add_entities(entities)
class BWTSensor(CoordinatorEntity, SensorEntity):
"""Representation of a BWT sensor."""
def __init__(
self,
coordinator: BWTDataUpdateCoordinator,
entry: ConfigEntry,
sensor_type: str,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._sensor_type = sensor_type
self._serial_number = entry.data[CONF_SERIAL_NUMBER]
self._device_name = entry.data.get(CONF_DEVICE_NAME, DEFAULT_DEVICE_NAME)
self._attr_unique_id = f"{self._serial_number}_{sensor_type}"
sensor_info = SENSOR_TYPES[sensor_type]
self._attr_name = f"{self._device_name} {sensor_info['name']}"
self._attr_native_unit_of_measurement = sensor_info["unit"]
self._attr_icon = sensor_info["icon"]
self._attr_device_class = sensor_info.get("device_class")
self._attr_state_class = sensor_info.get("state_class")
# Device info for grouping
self._attr_device_info = {
"identifiers": {(DOMAIN, self._serial_number)},
"name": self._device_name,
"manufacturer": MANUFACTURER,
"model": "Adoucisseur BWT",
}
@property
def native_value(self):
"""Return the state of the sensor."""
if self.coordinator.data is None:
return None
return self.coordinator.data.get(self._sensor_type)
@property
def available(self) -> bool:
"""Return if entity is available."""
return self.coordinator.last_update_success and self.coordinator.data is not None