ha-core/homeassistant/components/goalzero/sensor.py

154 lines
4.7 KiB
Python
Raw Normal View History

"""Support for Goal Zero Yeti Sensors."""
from __future__ import annotations
2021-10-15 00:20:08 +02:00
from typing import cast
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ELECTRIC_POTENTIAL_VOLT,
ENERGY_WATT_HOUR,
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS,
TEMP_CELSIUS,
TIME_MINUTES,
TIME_SECONDS,
UnitOfElectricCurrent,
UnitOfPower,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
2022-06-05 03:50:38 +02:00
from .const import DOMAIN
from .entity import GoalZeroEntity
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key="wattsIn",
name="Watts in",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="ampsIn",
name="Amps in",
device_class=SensorDeviceClass.CURRENT,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="wattsOut",
name="Watts out",
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="ampsOut",
name="Amps out",
device_class=SensorDeviceClass.CURRENT,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state_class=SensorStateClass.MEASUREMENT,
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="whOut",
name="Wh out",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ENERGY_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="whStored",
name="Wh stored",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ENERGY_WATT_HOUR,
state_class=SensorStateClass.MEASUREMENT,
),
SensorEntityDescription(
key="volts",
name="Volts",
device_class=SensorDeviceClass.VOLTAGE,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
entity_registry_enabled_default=False,
),
SensorEntityDescription(
key="socPercent",
name="State of charge percent",
device_class=SensorDeviceClass.BATTERY,
native_unit_of_measurement=PERCENTAGE,
),
SensorEntityDescription(
key="timeToEmptyFull",
name="Time to empty/full",
device_class=SensorDeviceClass.DURATION,
native_unit_of_measurement=TIME_MINUTES,
),
SensorEntityDescription(
key="temperature",
name="Temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="wifiStrength",
name="Wi-Fi strength",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="timestamp",
name="Total run time",
native_unit_of_measurement=TIME_SECONDS,
entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="ssid",
name="Wi-Fi SSID",
entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC,
),
SensorEntityDescription(
key="ipAddr",
name="IP address",
entity_registry_enabled_default=False,
entity_category=EntityCategory.DIAGNOSTIC,
),
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the Goal Zero Yeti sensor."""
2022-06-05 03:50:38 +02:00
async_add_entities(
GoalZeroSensor(
hass.data[DOMAIN][entry.entry_id],
description,
)
for description in SENSOR_TYPES
2022-06-05 03:50:38 +02:00
)
2022-06-05 03:50:38 +02:00
class GoalZeroSensor(GoalZeroEntity, SensorEntity):
"""Representation of a Goal Zero Yeti sensor."""
@property
def native_value(self) -> StateType:
"""Return the state."""
2022-06-05 03:50:38 +02:00
return cast(StateType, self._api.data[self.entity_description.key])