Skip to content

Commit ba58e52

Browse files
authored
Merge pull request #10 from golles/add-wind-chill-+-improvements
Add wind chill
2 parents 8aa32f0 + 543e043 commit ba58e52

File tree

7 files changed

+145
-19
lines changed

7 files changed

+145
-19
lines changed

custom_components/knmi/binary_sensor.py

+40-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ async def async_setup_entry(hass, entry, async_add_devices):
1414
coordinator = hass.data[DOMAIN][entry.entry_id]
1515
sensors: list[KnmiBinarySensor] = []
1616
for sensor in BINARY_SENSORS:
17-
sensors.append(KnmiBinarySensor(coordinator, entry, sensor["name"], sensor["unit"], sensor["icon"], sensor["key"]))
17+
sensors.append(
18+
KnmiBinarySensor(
19+
coordinator,
20+
entry,
21+
sensor.get("name", None),
22+
sensor.get("icon", None),
23+
sensor.get("device_class", None),
24+
sensor.get("attributes", []),
25+
sensor.get("key", None),
26+
)
27+
)
1828

1929
async_add_devices(sensors)
2030

@@ -23,14 +33,22 @@ class KnmiBinarySensor(KnmiEntity, BinarySensorEntity):
2333
"""knmi binary_sensor class."""
2434

2535
def __init__(
26-
self, coordinator, config_entry, name, unit_of_measurement, icon, data_key
36+
self,
37+
coordinator,
38+
config_entry,
39+
name,
40+
icon,
41+
device_class,
42+
attributes,
43+
data_key,
2744
):
2845
super().__init__(coordinator, config_entry)
2946
self.config_entry = config_entry
3047
self.location_name = self.coordinator.data["plaats"]
3148
self._name = name
32-
self._unit_of_measurement = unit_of_measurement
3349
self._icon = icon
50+
self._device_class = device_class
51+
self._attributes = attributes
3452
self._data_key = data_key
3553

3654
@property
@@ -43,12 +61,26 @@ def is_on(self):
4361
"""Return true if the binary_sensor is on."""
4462
return self.coordinator.data[self._data_key] != "0"
4563

46-
@property
47-
def extra_state_attributes(self):
48-
"""Return the device state attributes."""
49-
return {self._name: self.coordinator.data["alarmtxt"]}
50-
5164
@property
5265
def icon(self):
5366
"""Return the icon of the sensor."""
5467
return self._icon
68+
69+
@property
70+
def device_class(self):
71+
"""Return the device class."""
72+
return self._device_class
73+
74+
@property
75+
def extra_state_attributes(self):
76+
"""Return the device state attributes."""
77+
attributes = super().extra_state_attributes
78+
for attribute in self._attributes:
79+
value = None
80+
if "key" in attribute:
81+
value = self.coordinator.data[attribute.get("key", None)]
82+
if "value" in attribute:
83+
value = attribute.get("value", None)
84+
attributes[attribute.get("name", None)] = value
85+
86+
return attributes

custom_components/knmi/const.py

+60-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
"""Constants for knmi."""
22

3+
from homeassistant.const import (
4+
DEVICE_CLASS_TEMPERATURE,
5+
TEMP_CELSIUS,
6+
)
7+
from homeassistant.components.binary_sensor import (
8+
DEVICE_CLASS_SAFETY,
9+
)
310
from homeassistant.components.weather import (
411
ATTR_CONDITION_CLEAR_NIGHT,
512
ATTR_CONDITION_CLOUDY,
@@ -13,10 +20,13 @@
1320
ATTR_CONDITION_SUNNY,
1421
)
1522

23+
# Todo, import next: from homeassistant.components.sensor import SensorStateClass
24+
MEASUREMENT = "measurement"
25+
1626
# Base component constants.
1727
NAME = "KNMI"
1828
DOMAIN = "knmi"
19-
VERSION = "1.1.3"
29+
VERSION = "1.1.4"
2030
ATTRIBUTION = "KNMI Weergegevens via https://weerlive.nl/"
2131

2232
# Platforms.
@@ -27,14 +37,59 @@
2737

2838
# Binary sensors
2939
BINARY_SENSORS = [
30-
{"name": "Waarschuwing", "unit": "", "icon": "mdi:alert", "key": "alarm"}
40+
{
41+
"name": "Waarschuwing",
42+
"unit": "",
43+
"icon": "mdi:alert",
44+
"key": "alarm",
45+
"device_class": DEVICE_CLASS_SAFETY,
46+
"attributes": [
47+
{
48+
"name": "Waarschuwing",
49+
"key": "alarmtxt",
50+
},
51+
],
52+
},
3153
]
3254

3355
# Sensors
3456
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"},
57+
{
58+
"name": "Omschrijving",
59+
"icon": "mdi:text",
60+
"key": "samenv",
61+
},
62+
{
63+
"name": "Korte dagverwachting",
64+
"icon": "mdi:text",
65+
"key": "verw",
66+
},
67+
{
68+
"name": "Dauwpunt",
69+
"unit_of_measurement": TEMP_CELSIUS,
70+
"icon": "mdi:thermometer",
71+
"key": "dauwp",
72+
"device_class": DEVICE_CLASS_TEMPERATURE,
73+
"attributes": [
74+
{
75+
"name": "state_class",
76+
"value": MEASUREMENT,
77+
},
78+
],
79+
},
80+
{
81+
"name": "Gevoelstemperatuur",
82+
"unit_of_measurement": TEMP_CELSIUS,
83+
"icon": "mdi:thermometer",
84+
"key": "gtemp",
85+
"device_class": DEVICE_CLASS_TEMPERATURE,
86+
"attributes": [
87+
{
88+
"name": "state_class",
89+
"value": MEASUREMENT,
90+
},
91+
],
92+
},
3893
]
3994

4095
# Defaults

custom_components/knmi/entity.py

-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,5 @@ def extra_state_attributes(self):
3131
"""Return the state attributes."""
3232
return {
3333
"attribution": ATTRIBUTION,
34-
"id": str(self.coordinator.data.get("id")),
3534
"integration": DOMAIN,
3635
}

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.1.3",
7+
"version": "1.1.4",
88
"config_flow": true,
99
"codeowners": [
1010
"@golles"

custom_components/knmi/sensor.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@ async def async_setup_entry(hass, entry, async_add_devices):
99

1010
sensors: list[KnmiSensor] = []
1111
for sensor in SENSORS:
12-
sensors.append(KnmiSensor(coordinator, entry, sensor["name"], sensor["unit"], sensor["icon"], sensor["key"]))
12+
sensors.append(
13+
KnmiSensor(
14+
coordinator,
15+
entry,
16+
sensor.get("name", None),
17+
sensor.get("unit_of_measurement", None),
18+
sensor.get("icon", None),
19+
sensor.get("device_class", None),
20+
sensor.get("attributes", []),
21+
sensor.get("key", None),
22+
)
23+
)
1324

1425
async_add_devices(sensors)
1526

@@ -18,14 +29,24 @@ class KnmiSensor(KnmiEntity):
1829
"""Knmi Sensor class."""
1930

2031
def __init__(
21-
self, coordinator, config_entry, name, unit_of_measurement, icon, data_key
32+
self,
33+
coordinator,
34+
config_entry,
35+
name,
36+
unit_of_measurement,
37+
icon,
38+
device_class,
39+
attributes,
40+
data_key,
2241
):
2342
super().__init__(coordinator, config_entry)
2443
self.config_entry = config_entry
2544
self.location_name = self.coordinator.data["plaats"]
2645
self._name = name
2746
self._unit_of_measurement = unit_of_measurement
2847
self._icon = icon
48+
self._device_class = device_class
49+
self._attributes = attributes
2950
self._data_key = data_key
3051

3152
@property
@@ -47,3 +68,22 @@ def unit_of_measurement(self):
4768
def icon(self):
4869
"""Return the icon of the sensor."""
4970
return self._icon
71+
72+
@property
73+
def device_class(self):
74+
"""Return the device class."""
75+
return self._device_class
76+
77+
@property
78+
def extra_state_attributes(self):
79+
"""Return the device state attributes."""
80+
attributes = super().extra_state_attributes
81+
for attribute in self._attributes:
82+
value = None
83+
if "key" in attribute:
84+
value = self.coordinator.data[attribute.get("key", None)]
85+
if "value" in attribute:
86+
value = attribute.get("value", None)
87+
attributes[attribute.get("name", None)] = value
88+
89+
return attributes

tests/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ source venv/bin/activate
1111
```
1212

1313
You can then install the dependencies that will allow you to run tests:
14-
`pip3 install -r requirements_test.txt.`
14+
`pip3 install -r requirements_test.txt`.
1515

1616
This will install `homeassistant`, `pytest`, and `pytest-homeassistant-custom-component`, a plugin which allows you to leverage helpers that are available in Home Assistant for core integration tests.
1717

tests/test_api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import asyncio
33

44
import aiohttp
5-
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
5+
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
66
from homeassistant.helpers.aiohttp_client import async_get_clientsession
77

88
from custom_components.knmi.api import KnmiApiClient

0 commit comments

Comments
 (0)