hass-core/homeassistant/components/nibe_heatpump/sensor.py
Joakim Plate f5120872aa
Support for nibe heat pumps with local access (#78542)
* Add nibe local integration

* Add sensor platform

* Enable sensor platform

* Fix manifest

* Correct domain after rename

* Adjust tests for rename

* Correct codeowners

* Add requirements for tests

* Grab coil by name

* Switch to home assistant error

* Config entry always exist

* Switch to create task

* Bump to 0.5.0

* Use new coils access

* Remove unneeded check

* Use single instance of logger

* Test invalid ip

* Don't allow coil to be None

* Remove sleep

* Initialize data in coordinator init

* Add utils to ignore

* Update homeassistant/components/nibe_heatpump/manifest.json

Co-authored-by: J. Nick Koston <nick@koston.org>

* Use generator instead

* Use tenacity as retry decorator

* Use package instead of name to get logger

* Skip broad exception handling

* Catch missing coil exception

* Add missing test

Co-authored-by: J. Nick Koston <nick@koston.org>
2022-09-22 08:43:30 +02:00

77 lines
2.4 KiB
Python

"""The Nibe Heat Pump sensors."""
from __future__ import annotations
from nibe.coil import Coil
from homeassistant.components.sensor import (
ENTITY_ID_FORMAT,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ELECTRIC_CURRENT_AMPERE,
ELECTRIC_CURRENT_MILLIAMPERE,
ELECTRIC_POTENTIAL_MILLIVOLT,
ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR,
ENERGY_MEGA_WATT_HOUR,
ENERGY_WATT_HOUR,
TEMP_CELSIUS,
TEMP_FAHRENHEIT,
TEMP_KELVIN,
TIME_HOURS,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import DOMAIN, CoilEntity, Coordinator
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up platform."""
coordinator: Coordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities(
Sensor(coordinator, coil)
for coil in coordinator.coils
if not coil.is_writable and not coil.is_boolean
)
class Sensor(SensorEntity, CoilEntity):
"""Sensor entity."""
_attr_entity_category = EntityCategory.DIAGNOSTIC
def __init__(self, coordinator: Coordinator, coil: Coil) -> None:
"""Initialize entity."""
super().__init__(coordinator, coil, ENTITY_ID_FORMAT)
self._attr_native_unit_of_measurement = coil.unit
unit = self.native_unit_of_measurement
if unit in {TEMP_CELSIUS, TEMP_FAHRENHEIT, TEMP_KELVIN}:
self._attr_device_class = SensorDeviceClass.TEMPERATURE
elif unit in {ELECTRIC_CURRENT_AMPERE, ELECTRIC_CURRENT_MILLIAMPERE}:
self._attr_device_class = SensorDeviceClass.CURRENT
elif unit in {ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_MILLIVOLT}:
self._attr_device_class = SensorDeviceClass.VOLTAGE
elif unit in {ENERGY_WATT_HOUR, ENERGY_KILO_WATT_HOUR, ENERGY_MEGA_WATT_HOUR}:
self._attr_device_class = SensorDeviceClass.ENERGY
elif unit in {TIME_HOURS}:
self._attr_device_class = SensorDeviceClass.DURATION
else:
self._attr_device_class = None
if unit:
self._attr_state_class = SensorStateClass.MEASUREMENT
def _async_read_coil(self, coil: Coil):
self._attr_native_value = coil.value