Skip to content

Commit

Permalink
Merge pull request #43 from golles/wind_beaufort
Browse files Browse the repository at this point in the history
Add wind sensor including support for Beaufort
  • Loading branch information
golles authored Nov 10, 2022
2 parents 6d275f8 + fc7abb4 commit 1578510
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
77 changes: 63 additions & 14 deletions custom_components/knmi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
CONF_NAME,
PERCENTAGE,
TEMP_CELSIUS,
UnitOfSpeed,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand All @@ -22,16 +23,6 @@
from .const import DEFAULT_NAME, DOMAIN

DESCRIPTIONS: list[SensorEntityDescription] = [
SensorEntityDescription(
key="samenv",
name="Omschrijving",
icon="mdi:text",
),
SensorEntityDescription(
key="verw",
name="Korte dagverwachting",
icon="mdi:text",
),
SensorEntityDescription(
key="dauwp",
name="Dauwpunt",
Expand All @@ -53,6 +44,16 @@
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="samenv",
name="Omschrijving",
icon="mdi:text",
),
SensorEntityDescription(
key="verw",
name="Korte dagverwachting",
icon="mdi:text",
),
]


Expand All @@ -65,16 +66,38 @@ async def async_setup_entry(
conf_name = entry.data.get(CONF_NAME, hass.config.location_name)
coordinator = hass.data[DOMAIN][entry.entry_id]

async_add_entities(
KnmiSensor(
entities: list[KnmiSensor] = []

# Add all meter sensors described above.
for description in DESCRIPTIONS:
entities.append(
KnmiSensor(
conf_name=conf_name,
coordinator=coordinator,
entry_id=entry.entry_id,
description=description,
)
)

# Add special wind sensor
entities.append(
KnmiWindSensor(
conf_name=conf_name,
coordinator=coordinator,
entry_id=entry.entry_id,
description=description,
description=SensorEntityDescription(
key="windkmh",
name="Wind",
icon="mdi:weather-windy",
native_unit_of_measurement=UnitOfSpeed.KILOMETERS_PER_HOUR,
device_class=SensorDeviceClass.SPEED,
state_class=SensorStateClass.MEASUREMENT,
),
)
for description in DESCRIPTIONS
)

async_add_entities(entities)


class KnmiSensor(CoordinatorEntity[KnmiDataUpdateCoordinator], SensorEntity):
"""Defines a KNMI sensor."""
Expand Down Expand Up @@ -102,3 +125,29 @@ def __init__(
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return self.coordinator.get_value(self.entity_description.key)


class KnmiWindSensor(KnmiSensor):
"""Defines a KNMI wind sensor."""

def __init__(
self,
conf_name: str,
coordinator: KnmiDataUpdateCoordinator,
entry_id: str,
description: SensorEntityDescription,
) -> None:
"""Initialize KNMI wind sensor."""
super().__init__(
conf_name=conf_name,
coordinator=coordinator,
entry_id=entry_id,
description=description,
)

self._attr_extra_state_attributes = {
"richting": self.coordinator.get_value("windr"),
"graden": self.coordinator.get_value("windrgr", int),
"beaufort": self.coordinator.get_value("winds", float),
"knopen": self.coordinator.get_value("windk", float),
}
5 changes: 4 additions & 1 deletion custom_components/knmi/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def forecast(self) -> list[Forecast] | None:
)
wind_speed = self.coordinator.get_value(f"d{i}windkmh", float)
sun_chance = self.coordinator.get_value(f"d{i}zon", float)
wind_speed_bft = self.coordinator.get_value(f"d{i}windk", float)
next_day = {
ATTR_FORECAST_TIME: date.isoformat(),
ATTR_FORECAST_CONDITION: condition,
Expand All @@ -166,7 +167,9 @@ def forecast(self) -> list[Forecast] | None:
ATTR_FORECAST_PRECIPITATION_PROBABILITY: precipitation_probability,
ATTR_FORECAST_WIND_BEARING: wind_bearing,
ATTR_FORECAST_WIND_SPEED: wind_speed,
"sun_chance": sun_chance, # Not officially supported, but nice addition.
# Not officially supported, but nice additions.
"wind_speed_bft": wind_speed_bft,
"sun_chance": sun_chance,
}
forecast.append(next_day)

Expand Down

0 comments on commit 1578510

Please sign in to comment.