Skip to content

Commit 149697f

Browse files
committed
Adding extra sensors from the API
As requested in #3
1 parent eb90c5c commit 149697f

File tree

7 files changed

+93
-20
lines changed

7 files changed

+93
-20
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ _Component to integrate with [knmi][knmi]._
1818
Platform | Description
1919
-- | --
2020
`binary_sensor` | Weather alert `True` or `False`, the alert itself is an attribute.
21+
`sensor` | a few weather related sensors.
2122
`weather` | Weather data provided by KNMI, https://weerlive.nl/.
2223

2324
## Installation
@@ -40,7 +41,9 @@ custom_components/knmi/api.py
4041
custom_components/knmi/binary_sensor.py
4142
custom_components/knmi/config_flow.py
4243
custom_components/knmi/const.py
44+
custom_components/knmi/entity.py
4345
custom_components/knmi/manifest.json
46+
custom_components/knmi/sensor.py
4447
custom_components/knmi/weather.py
4548
```
4649

custom_components/knmi/binary_sensor.py

+21-8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from homeassistant.components.binary_sensor import BinarySensorEntity
33

44
from .const import (
5-
BINARY_SENSOR_ALARM_ICON,
6-
BINARY_SENSOR_ALARM_NAME,
5+
BINARY_SENSORS,
76
DEFAULT_NAME,
87
DOMAIN,
98
)
@@ -13,29 +12,43 @@
1312
async def async_setup_entry(hass, entry, async_add_devices):
1413
"""Setup binary_sensor platform."""
1514
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)
1720

1821

1922
class KnmiBinarySensor(KnmiEntity, BinarySensorEntity):
2023
"""knmi binary_sensor class."""
2124

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+
2236
@property
2337
def name(self):
2438
"""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}"
2740

2841
@property
2942
def is_on(self):
3043
"""Return true if the binary_sensor is on."""
31-
return self.coordinator.data["alarm"] != "0"
44+
return self.coordinator.data[self._data_key] != "0"
3245

3346
@property
3447
def extra_state_attributes(self):
3548
"""Return the device state attributes."""
36-
return {BINARY_SENSOR_ALARM_NAME: self.coordinator.data["alarmtxt"]}
49+
return {self._name: self.coordinator.data["alarmtxt"]}
3750

3851
@property
3952
def icon(self):
4053
"""Return the icon of the sensor."""
41-
return BINARY_SENSOR_ALARM_ICON
54+
return self._icon

custom_components/knmi/const.py

+15-8
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,26 @@
1616
# Base component constants.
1717
NAME = "KNMI"
1818
DOMAIN = "knmi"
19-
VERSION = "1.0.6"
19+
VERSION = "1.1.0"
2020
ATTRIBUTION = "KNMI Weergegevens via https://weerlive.nl/"
2121

22-
# Icons.
23-
BINARY_SENSOR_ALARM_ICON = "mdi:alert"
24-
25-
# Sensor names.
26-
BINARY_SENSOR_ALARM_NAME = "Waarschuwing"
27-
2822
# Platforms.
2923
BINARY_SENSOR = "binary_sensor"
24+
SENSOR = "sensor"
3025
WEATHER = "weather"
31-
PLATFORMS = [BINARY_SENSOR, WEATHER]
26+
PLATFORMS = [BINARY_SENSOR, SENSOR, WEATHER]
27+
28+
# Binary sensors
29+
BINARY_SENSORS = [
30+
{"name": "Waarschuwing", "unit": "", "icon": "mdi:alert", "key": "alarmtxt"}
31+
]
32+
33+
# Sensors
34+
SENSORS = [
35+
{"name": "Omschrijving", "unit": "", "icon": "mdi:text", "key": "samenv"},
36+
{"name": "Korte dagverwachting", "unit": "", "icon": "mdi:text", "key": "verw"},
37+
{"name": "Dauwpunt", "unit": "°C", "icon": "mdi:thermometer", "key": "dauwp"},
38+
]
3239

3340
# Defaults
3441
DEFAULT_NAME = NAME

custom_components/knmi/entity.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ def __init__(self, coordinator, config_entry):
1212
@property
1313
def unique_id(self):
1414
"""Return a unique ID to use for this entity."""
15-
return self.config_entry.entry_id
15+
return f"{self.config_entry.entry_id}-{self.name}"
1616

1717
@property
1818
def device_info(self):
1919
return {
20-
"identifiers": {(DOMAIN, self.unique_id)},
20+
"identifiers": {(DOMAIN, self.config_entry.entry_id)},
2121
"name": NAME,
2222
"model": VERSION,
2323
"manufacturer": NAME,

custom_components/knmi/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"documentation": "https://github.com/golles/ha-knmi/",
55
"iot_class": "cloud_polling",
66
"issue_tracker": "https://github.com/golles/ha-knmi//issues",
7-
"version": "1.0.6",
7+
"version": "1.1.0",
88
"config_flow": true,
99
"codeowners": [
1010
"@golles"

custom_components/knmi/sensor.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Sensor platform for knmi."""
2+
from .const import DEFAULT_NAME, DOMAIN, SENSORS
3+
from .entity import KnmiEntity
4+
5+
6+
async def async_setup_entry(hass, entry, async_add_devices):
7+
"""Setup sensor platform."""
8+
coordinator = hass.data[DOMAIN][entry.entry_id]
9+
10+
sensors: list[KnmiSensor] = []
11+
for sensor in SENSORS:
12+
sensors.append(KnmiSensor(coordinator, entry, sensor["name"], sensor["unit"], sensor["icon"], sensor["key"]))
13+
14+
async_add_devices(sensors)
15+
16+
17+
class KnmiSensor(KnmiEntity):
18+
"""Knmi Sensor class."""
19+
20+
def __init__(
21+
self, coordinator, config_entry, name, unit_of_measurement, icon, data_key
22+
):
23+
super().__init__(coordinator, config_entry)
24+
self.config_entry = config_entry
25+
self.location_name = self.coordinator.data["plaats"]
26+
self._name = name
27+
self._unit_of_measurement = unit_of_measurement
28+
self._icon = icon
29+
self._data_key = data_key
30+
31+
@property
32+
def name(self):
33+
"""Return the name of the sensor."""
34+
return f"{DEFAULT_NAME} {self.location_name} {self._name}"
35+
36+
@property
37+
def state(self):
38+
"""Return the state of the sensor."""
39+
return self.coordinator.data[self._data_key]
40+
41+
@property
42+
def unit_of_measurement(self):
43+
"""Return the unit of measurement of this entity, if any."""
44+
return self._unit_of_measurement
45+
46+
@property
47+
def icon(self):
48+
"""Return the icon of the sensor."""
49+
return self._icon

tests/test_config_flow.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
BINARY_SENSOR,
1111
DOMAIN,
1212
PLATFORMS,
13+
SENSOR,
1314
WEATHER,
1415
)
1516

@@ -106,4 +107,4 @@ async def test_options_flow(hass):
106107
assert result["title"] == MOCK_CONFIG[CONF_NAME]
107108

108109
# Verify that the options were updated
109-
assert entry.options == {BINARY_SENSOR: True, WEATHER: False,}
110+
assert entry.options == {BINARY_SENSOR: True, SENSOR: True, WEATHER: False,}

0 commit comments

Comments
 (0)