|
2 | 2 | from homeassistant.components.binary_sensor import BinarySensorEntity
|
3 | 3 |
|
4 | 4 | from .const import (
|
5 |
| - BINARY_SENSOR_ALARM_ICON, |
6 |
| - BINARY_SENSOR_ALARM_NAME, |
| 5 | + BINARY_SENSORS, |
7 | 6 | DEFAULT_NAME,
|
8 | 7 | DOMAIN,
|
9 | 8 | )
|
|
13 | 12 | async def async_setup_entry(hass, entry, async_add_devices):
|
14 | 13 | """Setup binary_sensor platform."""
|
15 | 14 | coordinator = hass.data[DOMAIN][entry.entry_id]
|
16 |
| - async_add_devices([KnmiBinarySensor(coordinator, entry)]) |
| 15 | + sensors: list[KnmiBinarySensor] = [] |
| 16 | + for sensor in BINARY_SENSORS: |
| 17 | + sensors.append(KnmiBinarySensor(coordinator, entry, sensor["name"], sensor["unit"], sensor["icon"], sensor["key"])) |
| 18 | + |
| 19 | + async_add_devices(sensors) |
17 | 20 |
|
18 | 21 |
|
19 | 22 | class KnmiBinarySensor(KnmiEntity, BinarySensorEntity):
|
20 | 23 | """knmi binary_sensor class."""
|
21 | 24 |
|
| 25 | + def __init__( |
| 26 | + self, coordinator, config_entry, name, unit_of_measurement, icon, data_key |
| 27 | + ): |
| 28 | + super().__init__(coordinator, config_entry) |
| 29 | + self.config_entry = config_entry |
| 30 | + self.location_name = self.coordinator.data["plaats"] |
| 31 | + self._name = name |
| 32 | + self._unit_of_measurement = unit_of_measurement |
| 33 | + self._icon = icon |
| 34 | + self._data_key = data_key |
| 35 | + |
22 | 36 | @property
|
23 | 37 | def name(self):
|
24 | 38 | """Return the name of the binary_sensor."""
|
25 |
| - location = self.coordinator.data["plaats"] |
26 |
| - return f"{DEFAULT_NAME} {location} {BINARY_SENSOR_ALARM_NAME}" |
| 39 | + return f"{DEFAULT_NAME} {self.location_name} {self._name}" |
27 | 40 |
|
28 | 41 | @property
|
29 | 42 | def is_on(self):
|
30 | 43 | """Return true if the binary_sensor is on."""
|
31 |
| - return self.coordinator.data["alarm"] != "0" |
| 44 | + return self.coordinator.data[self._data_key] != "0" |
32 | 45 |
|
33 | 46 | @property
|
34 | 47 | def extra_state_attributes(self):
|
35 | 48 | """Return the device state attributes."""
|
36 |
| - return {BINARY_SENSOR_ALARM_NAME: self.coordinator.data["alarmtxt"]} |
| 49 | + return {self._name: self.coordinator.data["alarmtxt"]} |
37 | 50 |
|
38 | 51 | @property
|
39 | 52 | def icon(self):
|
40 | 53 | """Return the icon of the sensor."""
|
41 |
| - return BINARY_SENSOR_ALARM_ICON |
| 54 | + return self._icon |
0 commit comments