Skip to content

Commit

Permalink
Merge pull request #10 from golles/add-wind-chill-+-improvements
Browse files Browse the repository at this point in the history
Add wind chill
  • Loading branch information
golles authored Dec 14, 2021
2 parents 8aa32f0 + 543e043 commit ba58e52
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 19 deletions.
48 changes: 40 additions & 8 deletions custom_components/knmi/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@ async def async_setup_entry(hass, entry, async_add_devices):
coordinator = hass.data[DOMAIN][entry.entry_id]
sensors: list[KnmiBinarySensor] = []
for sensor in BINARY_SENSORS:
sensors.append(KnmiBinarySensor(coordinator, entry, sensor["name"], sensor["unit"], sensor["icon"], sensor["key"]))
sensors.append(
KnmiBinarySensor(
coordinator,
entry,
sensor.get("name", None),
sensor.get("icon", None),
sensor.get("device_class", None),
sensor.get("attributes", []),
sensor.get("key", None),
)
)

async_add_devices(sensors)

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

def __init__(
self, coordinator, config_entry, name, unit_of_measurement, icon, data_key
self,
coordinator,
config_entry,
name,
icon,
device_class,
attributes,
data_key,
):
super().__init__(coordinator, config_entry)
self.config_entry = config_entry
self.location_name = self.coordinator.data["plaats"]
self._name = name
self._unit_of_measurement = unit_of_measurement
self._icon = icon
self._device_class = device_class
self._attributes = attributes
self._data_key = data_key

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

@property
def extra_state_attributes(self):
"""Return the device state attributes."""
return {self._name: self.coordinator.data["alarmtxt"]}

@property
def icon(self):
"""Return the icon of the sensor."""
return self._icon

@property
def device_class(self):
"""Return the device class."""
return self._device_class

@property
def extra_state_attributes(self):
"""Return the device state attributes."""
attributes = super().extra_state_attributes
for attribute in self._attributes:
value = None
if "key" in attribute:
value = self.coordinator.data[attribute.get("key", None)]
if "value" in attribute:
value = attribute.get("value", None)
attributes[attribute.get("name", None)] = value

return attributes
65 changes: 60 additions & 5 deletions custom_components/knmi/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
"""Constants for knmi."""

from homeassistant.const import (
DEVICE_CLASS_TEMPERATURE,
TEMP_CELSIUS,
)
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_SAFETY,
)
from homeassistant.components.weather import (
ATTR_CONDITION_CLEAR_NIGHT,
ATTR_CONDITION_CLOUDY,
Expand All @@ -13,10 +20,13 @@
ATTR_CONDITION_SUNNY,
)

# Todo, import next: from homeassistant.components.sensor import SensorStateClass
MEASUREMENT = "measurement"

# Base component constants.
NAME = "KNMI"
DOMAIN = "knmi"
VERSION = "1.1.3"
VERSION = "1.1.4"
ATTRIBUTION = "KNMI Weergegevens via https://weerlive.nl/"

# Platforms.
Expand All @@ -27,14 +37,59 @@

# Binary sensors
BINARY_SENSORS = [
{"name": "Waarschuwing", "unit": "", "icon": "mdi:alert", "key": "alarm"}
{
"name": "Waarschuwing",
"unit": "",
"icon": "mdi:alert",
"key": "alarm",
"device_class": DEVICE_CLASS_SAFETY,
"attributes": [
{
"name": "Waarschuwing",
"key": "alarmtxt",
},
],
},
]

# Sensors
SENSORS = [
{"name": "Omschrijving", "unit": "", "icon": "mdi:text", "key": "samenv"},
{"name": "Korte dagverwachting", "unit": "", "icon": "mdi:text", "key": "verw"},
{"name": "Dauwpunt", "unit": "°C", "icon": "mdi:thermometer", "key": "dauwp"},
{
"name": "Omschrijving",
"icon": "mdi:text",
"key": "samenv",
},
{
"name": "Korte dagverwachting",
"icon": "mdi:text",
"key": "verw",
},
{
"name": "Dauwpunt",
"unit_of_measurement": TEMP_CELSIUS,
"icon": "mdi:thermometer",
"key": "dauwp",
"device_class": DEVICE_CLASS_TEMPERATURE,
"attributes": [
{
"name": "state_class",
"value": MEASUREMENT,
},
],
},
{
"name": "Gevoelstemperatuur",
"unit_of_measurement": TEMP_CELSIUS,
"icon": "mdi:thermometer",
"key": "gtemp",
"device_class": DEVICE_CLASS_TEMPERATURE,
"attributes": [
{
"name": "state_class",
"value": MEASUREMENT,
},
],
},
]

# Defaults
Expand Down
1 change: 0 additions & 1 deletion custom_components/knmi/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ def extra_state_attributes(self):
"""Return the state attributes."""
return {
"attribution": ATTRIBUTION,
"id": str(self.coordinator.data.get("id")),
"integration": DOMAIN,
}
2 changes: 1 addition & 1 deletion custom_components/knmi/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"documentation": "https://github.com/golles/ha-knmi/",
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/golles/ha-knmi//issues",
"version": "1.1.3",
"version": "1.1.4",
"config_flow": true,
"codeowners": [
"@golles"
Expand Down
44 changes: 42 additions & 2 deletions custom_components/knmi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ async def async_setup_entry(hass, entry, async_add_devices):

sensors: list[KnmiSensor] = []
for sensor in SENSORS:
sensors.append(KnmiSensor(coordinator, entry, sensor["name"], sensor["unit"], sensor["icon"], sensor["key"]))
sensors.append(
KnmiSensor(
coordinator,
entry,
sensor.get("name", None),
sensor.get("unit_of_measurement", None),
sensor.get("icon", None),
sensor.get("device_class", None),
sensor.get("attributes", []),
sensor.get("key", None),
)
)

async_add_devices(sensors)

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

def __init__(
self, coordinator, config_entry, name, unit_of_measurement, icon, data_key
self,
coordinator,
config_entry,
name,
unit_of_measurement,
icon,
device_class,
attributes,
data_key,
):
super().__init__(coordinator, config_entry)
self.config_entry = config_entry
self.location_name = self.coordinator.data["plaats"]
self._name = name
self._unit_of_measurement = unit_of_measurement
self._icon = icon
self._device_class = device_class
self._attributes = attributes
self._data_key = data_key

@property
Expand All @@ -47,3 +68,22 @@ def unit_of_measurement(self):
def icon(self):
"""Return the icon of the sensor."""
return self._icon

@property
def device_class(self):
"""Return the device class."""
return self._device_class

@property
def extra_state_attributes(self):
"""Return the device state attributes."""
attributes = super().extra_state_attributes
for attribute in self._attributes:
value = None
if "key" in attribute:
value = self.coordinator.data[attribute.get("key", None)]
if "value" in attribute:
value = attribute.get("value", None)
attributes[attribute.get("name", None)] = value

return attributes
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ source venv/bin/activate
```

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

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.

Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import asyncio

import aiohttp
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from custom_components.knmi.api import KnmiApiClient
Expand Down

0 comments on commit ba58e52

Please sign in to comment.