Skip to content

Commit

Permalink
Add more sensors to PEGELONLINE (home-assistant#97295)
Browse files Browse the repository at this point in the history
* add further sensors

* adjust and improve tests

* add device classes were applicable

* fix doc string

* name for ph comes from device class

* use icon from device class for ph sensor
  • Loading branch information
mib1185 authored Jul 29, 2023
1 parent b7ed163 commit 750260b
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 129 deletions.
13 changes: 4 additions & 9 deletions homeassistant/components/pegel_online/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""DataUpdateCoordinator for pegel_online."""
import logging

from aiopegelonline import CONNECT_ERRORS, PegelOnline, Station
from aiopegelonline import CONNECT_ERRORS, PegelOnline, Station, StationMeasurements

from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import MIN_TIME_BETWEEN_UPDATES
from .model import PegelOnlineData

_LOGGER = logging.getLogger(__name__)


class PegelOnlineDataUpdateCoordinator(DataUpdateCoordinator[PegelOnlineData]):
class PegelOnlineDataUpdateCoordinator(DataUpdateCoordinator[StationMeasurements]):
"""DataUpdateCoordinator for the pegel_online integration."""

def __init__(
Expand All @@ -28,13 +27,9 @@ def __init__(
update_interval=MIN_TIME_BETWEEN_UPDATES,
)

async def _async_update_data(self) -> PegelOnlineData:
async def _async_update_data(self) -> StationMeasurements:
"""Fetch data from API endpoint."""
try:
water_level = await self.api.async_get_station_measurement(
self.station.uuid
)
return await self.api.async_get_station_measurements(self.station.uuid)
except CONNECT_ERRORS as err:
raise UpdateFailed(f"Failed to communicate with API: {err}") from err

return {"water_level": water_level}
2 changes: 1 addition & 1 deletion homeassistant/components/pegel_online/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"integration_type": "service",
"iot_class": "cloud_polling",
"loggers": ["aiopegelonline"],
"requirements": ["aiopegelonline==0.0.5"]
"requirements": ["aiopegelonline==0.0.6"]
}
11 changes: 0 additions & 11 deletions homeassistant/components/pegel_online/model.py

This file was deleted.

91 changes: 79 additions & 12 deletions homeassistant/components/pegel_online/sensor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""PEGELONLINE sensor entities."""
from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass

from aiopegelonline.models import CurrentMeasurement

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
Expand All @@ -17,15 +19,13 @@
from .const import DOMAIN
from .coordinator import PegelOnlineDataUpdateCoordinator
from .entity import PegelOnlineEntity
from .model import PegelOnlineData


@dataclass
class PegelOnlineRequiredKeysMixin:
"""Mixin for required keys."""

fn_native_unit: Callable[[PegelOnlineData], str]
fn_native_value: Callable[[PegelOnlineData], float]
measurement_key: str


@dataclass
Expand All @@ -36,24 +36,86 @@ class PegelOnlineSensorEntityDescription(


SENSORS: tuple[PegelOnlineSensorEntityDescription, ...] = (
PegelOnlineSensorEntityDescription(
key="air_temperature",
translation_key="air_temperature",
measurement_key="air_temperature",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
icon="mdi:thermometer-lines",
entity_registry_enabled_default=False,
),
PegelOnlineSensorEntityDescription(
key="clearance_height",
translation_key="clearance_height",
measurement_key="clearance_height",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DISTANCE,
icon="mdi:bridge",
),
PegelOnlineSensorEntityDescription(
key="oxygen_level",
translation_key="oxygen_level",
measurement_key="oxygen_level",
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:water-opacity",
entity_registry_enabled_default=False,
),
PegelOnlineSensorEntityDescription(
key="ph_value",
measurement_key="ph_value",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.PH,
entity_registry_enabled_default=False,
),
PegelOnlineSensorEntityDescription(
key="water_speed",
translation_key="water_speed",
measurement_key="water_speed",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.SPEED,
icon="mdi:waves-arrow-right",
entity_registry_enabled_default=False,
),
PegelOnlineSensorEntityDescription(
key="water_flow",
translation_key="water_flow",
measurement_key="water_flow",
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:waves",
entity_registry_enabled_default=False,
),
PegelOnlineSensorEntityDescription(
key="water_level",
translation_key="water_level",
measurement_key="water_level",
state_class=SensorStateClass.MEASUREMENT,
fn_native_unit=lambda data: data["water_level"].uom,
fn_native_value=lambda data: data["water_level"].value,
icon="mdi:waves-arrow-up",
),
PegelOnlineSensorEntityDescription(
key="water_temperature",
translation_key="water_temperature",
measurement_key="water_temperature",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
icon="mdi:thermometer-water",
entity_registry_enabled_default=False,
),
)


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the PEGELONLINE sensor."""
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator: PegelOnlineDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]

async_add_entities(
[PegelOnlineSensor(coordinator, description) for description in SENSORS]
[
PegelOnlineSensor(coordinator, description)
for description in SENSORS
if getattr(coordinator.data, description.measurement_key) is not None
]
)


Expand All @@ -71,9 +133,9 @@ def __init__(
super().__init__(coordinator)
self.entity_description = description
self._attr_unique_id = f"{self.station.uuid}_{description.key}"
self._attr_native_unit_of_measurement = self.entity_description.fn_native_unit(
coordinator.data
)

if description.device_class != SensorDeviceClass.PH:
self._attr_native_unit_of_measurement = self.measurement.uom

if self.station.latitude and self.station.longitude:
self._attr_extra_state_attributes.update(
Expand All @@ -83,7 +145,12 @@ def __init__(
}
)

@property
def measurement(self) -> CurrentMeasurement:
"""Return the measurement data of the entity."""
return getattr(self.coordinator.data, self.entity_description.measurement_key)

@property
def native_value(self) -> float:
"""Return the state of the device."""
return self.entity_description.fn_native_value(self.coordinator.data)
return self.measurement.value
18 changes: 18 additions & 0 deletions homeassistant/components/pegel_online/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,26 @@
},
"entity": {
"sensor": {
"air_temperature": {
"name": "Air temperature"
},
"clearance_height": {
"name": "Clearance height"
},
"oxygen_level": {
"name": "Oxygen level"
},
"water_speed": {
"name": "Water flow speed"
},
"water_flow": {
"name": "Water volume flow"
},
"water_level": {
"name": "Water level"
},
"water_temperature": {
"name": "Water temperature"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ aiooncue==0.3.5
aioopenexchangerates==0.4.0

# homeassistant.components.pegel_online
aiopegelonline==0.0.5
aiopegelonline==0.0.6

# homeassistant.components.acmeda
aiopulse==0.4.3
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ aiooncue==0.3.5
aioopenexchangerates==0.4.0

# homeassistant.components.pegel_online
aiopegelonline==0.0.5
aiopegelonline==0.0.6

# homeassistant.components.acmeda
aiopulse==0.4.3
Expand Down
10 changes: 5 additions & 5 deletions tests/components/pegel_online/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def __init__(
self,
nearby_stations=None,
station_details=None,
station_measurement=None,
station_measurements=None,
side_effect=None,
) -> None:
"""Init the mock."""
self.nearby_stations = nearby_stations
self.station_details = station_details
self.station_measurement = station_measurement
self.station_measurements = station_measurements
self.side_effect = side_effect

async def async_get_nearby_stations(self, *args):
Expand All @@ -29,11 +29,11 @@ async def async_get_station_details(self, *args):
raise self.side_effect
return self.station_details

async def async_get_station_measurement(self, *args):
"""Mock async_get_station_measurement."""
async def async_get_station_measurements(self, *args):
"""Mock async_get_station_measurements."""
if self.side_effect:
raise self.side_effect
return self.station_measurement
return self.station_measurements

def override_side_effect(self, side_effect):
"""Override the side_effect."""
Expand Down
Loading

0 comments on commit 750260b

Please sign in to comment.