2019-04-03 17:40:03 +02:00
|
|
|
"""Support for getting collected information from PVOutput."""
|
2021-08-06 12:56:27 -04:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-12-27 22:05:10 +01:00
|
|
|
from pvo import Status
|
2016-11-19 10:04:03 +01:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-08-05 10:09:56 -07:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
2021-12-16 16:25:04 -05:00
|
|
|
SensorDeviceClass,
|
2021-08-05 10:09:56 -07:00
|
|
|
SensorEntity,
|
2021-12-16 16:25:04 -05:00
|
|
|
SensorStateClass,
|
2021-08-05 10:09:56 -07:00
|
|
|
)
|
2021-12-27 22:05:10 +01:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2016-11-19 10:04:03 +01:00
|
|
|
from homeassistant.const import (
|
2019-12-09 14:29:39 +01:00
|
|
|
ATTR_TEMPERATURE,
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_VOLTAGE,
|
2019-12-09 14:29:39 +01:00
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_NAME,
|
2021-08-05 10:09:56 -07:00
|
|
|
ENERGY_WATT_HOUR,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2021-12-23 06:25:05 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-12-09 14:29:39 +01:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-12-23 06:25:05 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2021-12-27 22:05:10 +01:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2016-11-19 10:04:03 +01:00
|
|
|
|
2021-12-27 22:05:10 +01:00
|
|
|
from .const import (
|
|
|
|
ATTR_EFFICIENCY,
|
|
|
|
ATTR_ENERGY_CONSUMPTION,
|
|
|
|
ATTR_ENERGY_GENERATION,
|
|
|
|
ATTR_POWER_CONSUMPTION,
|
|
|
|
ATTR_POWER_GENERATION,
|
|
|
|
CONF_SYSTEM_ID,
|
|
|
|
DEFAULT_NAME,
|
|
|
|
DOMAIN,
|
|
|
|
LOGGER,
|
|
|
|
)
|
2017-06-05 16:59:59 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Required(CONF_SYSTEM_ID): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-11-19 10:04:03 +01:00
|
|
|
|
|
|
|
|
2021-12-23 06:25:05 +01:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2016-11-19 10:04:03 +01:00
|
|
|
"""Set up the PVOutput sensor."""
|
2021-12-27 22:05:10 +01:00
|
|
|
LOGGER.warning(
|
|
|
|
"Configuration of the PVOutput platform in YAML is deprecated and will be "
|
|
|
|
"removed in Home Assistant 2022.4; Your existing configuration "
|
|
|
|
"has been imported into the UI automatically and can be safely removed "
|
|
|
|
"from your configuration.yaml file"
|
|
|
|
)
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data={
|
|
|
|
CONF_SYSTEM_ID: config[CONF_SYSTEM_ID],
|
|
|
|
CONF_API_KEY: config[CONF_API_KEY],
|
|
|
|
CONF_NAME: config[CONF_NAME],
|
|
|
|
},
|
|
|
|
)
|
2021-12-22 21:17:23 +01:00
|
|
|
)
|
2016-11-19 10:04:03 +01:00
|
|
|
|
|
|
|
|
2021-12-27 22:05:10 +01:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up a Tailscale binary sensors based on a config entry."""
|
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
async_add_entities([PvoutputSensor(coordinator)])
|
2016-11-19 10:04:03 +01:00
|
|
|
|
|
|
|
|
2021-12-27 22:05:10 +01:00
|
|
|
class PvoutputSensor(CoordinatorEntity, SensorEntity):
|
2016-11-19 10:04:03 +01:00
|
|
|
"""Representation of a PVOutput sensor."""
|
|
|
|
|
2021-12-16 16:25:04 -05:00
|
|
|
_attr_state_class = SensorStateClass.TOTAL_INCREASING
|
|
|
|
_attr_device_class = SensorDeviceClass.ENERGY
|
2021-08-12 13:26:17 +02:00
|
|
|
_attr_native_unit_of_measurement = ENERGY_WATT_HOUR
|
2021-08-05 10:09:56 -07:00
|
|
|
|
2021-12-27 22:05:10 +01:00
|
|
|
coordinator: DataUpdateCoordinator[Status]
|
2016-11-19 10:04:03 +01:00
|
|
|
|
|
|
|
@property
|
2021-12-23 06:25:05 +01:00
|
|
|
def native_value(self) -> int | None:
|
2016-11-19 10:04:03 +01:00
|
|
|
"""Return the state of the device."""
|
2021-12-27 22:05:10 +01:00
|
|
|
return self.coordinator.data.energy_generation
|
2016-11-19 10:04:03 +01:00
|
|
|
|
|
|
|
@property
|
2021-12-23 06:25:05 +01:00
|
|
|
def extra_state_attributes(self) -> dict[str, int | float | None]:
|
2016-11-24 10:15:00 +01:00
|
|
|
"""Return the state attributes of the monitored installation."""
|
2021-12-22 21:17:23 +01:00
|
|
|
return {
|
2021-12-27 22:05:10 +01:00
|
|
|
ATTR_ENERGY_GENERATION: self.coordinator.data.energy_generation,
|
|
|
|
ATTR_POWER_GENERATION: self.coordinator.data.power_generation,
|
|
|
|
ATTR_ENERGY_CONSUMPTION: self.coordinator.data.energy_consumption,
|
|
|
|
ATTR_POWER_CONSUMPTION: self.coordinator.data.power_consumption,
|
|
|
|
ATTR_EFFICIENCY: self.coordinator.data.normalized_ouput,
|
|
|
|
ATTR_TEMPERATURE: self.coordinator.data.temperature,
|
|
|
|
ATTR_VOLTAGE: self.coordinator.data.voltage,
|
2021-12-22 21:17:23 +01:00
|
|
|
}
|