core/homeassistant/components/oncue/sensor.py

218 lines
7.2 KiB
Python

"""Support for Oncue sensors."""
from __future__ import annotations
from aiooncue import OncueDevice, OncueSensor
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import (
PERCENTAGE,
EntityCategory,
UnitOfElectricCurrent,
UnitOfElectricPotential,
UnitOfEnergy,
UnitOfFrequency,
UnitOfPower,
UnitOfPressure,
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .entity import OncueEntity
from .types import OncueConfigEntry
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="LatestFirmware",
icon="mdi:update",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="EngineSpeed",
icon="mdi:speedometer",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="EngineTargetSpeed",
icon="mdi:speedometer",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="EngineOilPressure",
native_unit_of_measurement=UnitOfPressure.PSI,
device_class=SensorDeviceClass.PRESSURE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="EngineCoolantTemperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="BatteryVoltage",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="LubeOilTemperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="GensetControllerTemperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="EngineCompartmentTemperature",
device_class=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="GeneratorTrueTotalPower",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="GeneratorTruePercentOfRatedPower",
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="GeneratorVoltageAverageLineToLine",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="GeneratorFrequency",
native_unit_of_measurement=UnitOfFrequency.HERTZ,
device_class=SensorDeviceClass.FREQUENCY,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(key="GensetState", icon="mdi:home-lightning-bolt"),
SensorEntityDescription(
key="GensetControllerTotalOperationTime",
icon="mdi:hours-24",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="EngineTotalRunTime",
icon="mdi:hours-24",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="EngineTotalRunTimeLoaded",
icon="mdi:hours-24",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(key="AtsContactorPosition", icon="mdi:electric-switch"),
SensorEntityDescription(
key="IPAddress",
icon="mdi:ip-network",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="ConnectedServerIPAddress",
icon="mdi:server-network",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="Source1VoltageAverageLineToLine",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="Source2VoltageAverageLineToLine",
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="GensetTotalEnergy",
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
),
SensorEntityDescription(
key="EngineTotalNumberOfStarts",
icon="mdi:engine",
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="GeneratorCurrentAverage",
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
),
)
SENSOR_MAP = {description.key: description for description in SENSOR_TYPES}
UNIT_MAPPINGS = {
"C": UnitOfTemperature.CELSIUS,
"F": UnitOfTemperature.FAHRENHEIT,
}
async def async_setup_entry(
hass: HomeAssistant,
config_entry: OncueConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up sensors."""
coordinator = config_entry.runtime_data
devices = coordinator.data
async_add_entities(
OncueSensorEntity(coordinator, device_id, device, sensor, SENSOR_MAP[key])
for device_id, device in devices.items()
for key, sensor in device.sensors.items()
if key in SENSOR_MAP
)
class OncueSensorEntity(OncueEntity, SensorEntity):
"""Representation of an Oncue sensor."""
def __init__(
self,
coordinator: DataUpdateCoordinator[dict[str, OncueDevice]],
device_id: str,
device: OncueDevice,
sensor: OncueSensor,
description: SensorEntityDescription,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, device_id, device, sensor, description)
if not description.native_unit_of_measurement and sensor.unit is not None:
self._attr_native_unit_of_measurement = UNIT_MAPPINGS.get(
sensor.unit, sensor.unit
)
@property
def native_value(self) -> str:
"""Return the sensors state."""
return self._oncue_value