Add configuration flow to PVOutput (#62667)

* Add configuration flow to PVOutput

* Update homeassistant/components/pvoutput/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/pvoutput/sensor.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Use account URL placeholder

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Franck Nijhof
2021-12-27 22:05:10 +01:00
committed by GitHub
co-authored by Martin Hjelmare
parent 6b0f2aa13f
commit 1cfeb404b6
14 changed files with 471 additions and 45 deletions
+42 -1
View File
@@ -1 +1,42 @@
"""The pvoutput component."""
"""The PVOutput integration."""
from __future__ import annotations
from pvo import PVOutput, Status
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_KEY
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import CONF_SYSTEM_ID, DOMAIN, LOGGER, PLATFORMS, SCAN_INTERVAL
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up PVOutput from a config entry."""
pvoutput = PVOutput(
api_key=entry.data[CONF_API_KEY],
system_id=entry.data[CONF_SYSTEM_ID],
session=async_get_clientsession(hass),
)
coordinator: DataUpdateCoordinator[Status] = DataUpdateCoordinator(
hass,
LOGGER,
name=f"{DOMAIN}_{entry.data[CONF_SYSTEM_ID]}",
update_interval=SCAN_INTERVAL,
update_method=pvoutput.status,
)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload PVOutput config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
del hass.data[DOMAIN][entry.entry_id]
return unload_ok
@@ -0,0 +1,92 @@
"""Config flow to configure the PVOutput integration."""
from __future__ import annotations
from typing import Any
from pvo import PVOutput, PVOutputAuthenticationError, PVOutputError
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow
from homeassistant.const import CONF_API_KEY, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_SYSTEM_ID, DOMAIN
async def validate_input(hass: HomeAssistant, *, api_key: str, system_id: int) -> None:
"""Try using the give system id & api key against the PVOutput API."""
session = async_get_clientsession(hass)
pvoutput = PVOutput(
session=session,
api_key=api_key,
system_id=system_id,
)
await pvoutput.status()
class TailscaleFlowHandler(ConfigFlow, domain=DOMAIN):
"""Config flow for PVOutput."""
VERSION = 1
imported_name: str | None = None
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initialized by the user."""
errors = {}
if user_input is not None:
try:
await validate_input(
self.hass,
api_key=user_input[CONF_API_KEY],
system_id=user_input[CONF_SYSTEM_ID],
)
except PVOutputAuthenticationError:
errors["base"] = "invalid_auth"
except PVOutputError:
errors["base"] = "cannot_connect"
else:
await self.async_set_unique_id(str(user_input[CONF_SYSTEM_ID]))
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=self.imported_name or str(user_input[CONF_SYSTEM_ID]),
data={
CONF_SYSTEM_ID: user_input[CONF_SYSTEM_ID],
CONF_API_KEY: user_input[CONF_API_KEY],
},
)
else:
user_input = {}
return self.async_show_form(
step_id="user",
description_placeholders={
"account_url": "https://pvoutput.org/account.jsp"
},
data_schema=vol.Schema(
{
vol.Required(
CONF_API_KEY, default=user_input.get(CONF_API_KEY, "")
): str,
vol.Required(
CONF_SYSTEM_ID, default=user_input.get(CONF_SYSTEM_ID, "")
): int,
}
),
errors=errors,
)
async def async_step_import(self, config: dict[str, Any]) -> FlowResult:
"""Handle a flow initialized by importing a config."""
self.imported_name = config[CONF_NAME]
return await self.async_step_user(
user_input={
CONF_SYSTEM_ID: config[CONF_SYSTEM_ID],
CONF_API_KEY: config[CONF_API_KEY],
}
)
@@ -0,0 +1,25 @@
"""Constants for the PVOutput integration."""
from __future__ import annotations
from datetime import timedelta
import logging
from typing import Final
from homeassistant.const import Platform
DOMAIN: Final = "pvoutput"
PLATFORMS = [Platform.SENSOR]
LOGGER = logging.getLogger(__package__)
SCAN_INTERVAL = timedelta(minutes=2)
ATTR_ENERGY_GENERATION = "energy_generation"
ATTR_POWER_GENERATION = "power_generation"
ATTR_ENERGY_CONSUMPTION = "energy_consumption"
ATTR_POWER_CONSUMPTION = "power_consumption"
ATTR_EFFICIENCY = "efficiency"
CONF_SYSTEM_ID = "system_id"
DEFAULT_NAME = "PVOutput"
@@ -2,6 +2,7 @@
"domain": "pvoutput",
"name": "PVOutput",
"documentation": "https://www.home-assistant.io/integrations/pvoutput",
"config_flow": true,
"codeowners": ["@fabaff", "@frenck"],
"requirements": ["pvo==0.1.0"],
"iot_class": "cloud_polling"
+51 -44
View File
@@ -1,10 +1,7 @@
"""Support for getting collected information from PVOutput."""
from __future__ import annotations
from datetime import timedelta
import logging
from pvo import PVOutput, PVOutputError, Status
from pvo import Status
import voluptuous as vol
from homeassistant.components.sensor import (
@@ -13,6 +10,7 @@ from homeassistant.components.sensor import (
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
ATTR_TEMPERATURE,
ATTR_VOLTAGE,
@@ -24,20 +22,22 @@ from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
_LOGGER = logging.getLogger(__name__)
ATTR_ENERGY_GENERATION = "energy_generation"
ATTR_POWER_GENERATION = "power_generation"
ATTR_ENERGY_CONSUMPTION = "energy_consumption"
ATTR_POWER_CONSUMPTION = "power_consumption"
ATTR_EFFICIENCY = "efficiency"
CONF_SYSTEM_ID = "system_id"
DEFAULT_NAME = "PVOutput"
SCAN_INTERVAL = timedelta(minutes=2)
from .const import (
ATTR_EFFICIENCY,
ATTR_ENERGY_CONSUMPTION,
ATTR_ENERGY_GENERATION,
ATTR_POWER_CONSUMPTION,
ATTR_POWER_GENERATION,
CONF_SYSTEM_ID,
DEFAULT_NAME,
DOMAIN,
LOGGER,
)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
@@ -55,51 +55,58 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the PVOutput sensor."""
pvoutput = PVOutput(
api_key=config[CONF_API_KEY],
system_id=config[CONF_SYSTEM_ID],
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],
},
)
)
try:
status = await pvoutput.status()
except PVOutputError:
_LOGGER.error("Unable to fetch data from PVOutput")
return
async_add_entities([PvoutputSensor(pvoutput, status, config[CONF_NAME])])
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)])
class PvoutputSensor(SensorEntity):
class PvoutputSensor(CoordinatorEntity, SensorEntity):
"""Representation of a PVOutput sensor."""
_attr_state_class = SensorStateClass.TOTAL_INCREASING
_attr_device_class = SensorDeviceClass.ENERGY
_attr_native_unit_of_measurement = ENERGY_WATT_HOUR
def __init__(self, pvoutput: PVOutput, status: Status, name: str) -> None:
"""Initialize a PVOutput sensor."""
self._attr_name = name
self.pvoutput = pvoutput
self.status = status
coordinator: DataUpdateCoordinator[Status]
@property
def native_value(self) -> int | None:
"""Return the state of the device."""
return self.status.energy_generation
return self.coordinator.data.energy_generation
@property
def extra_state_attributes(self) -> dict[str, int | float | None]:
"""Return the state attributes of the monitored installation."""
return {
ATTR_ENERGY_GENERATION: self.status.energy_generation,
ATTR_POWER_GENERATION: self.status.power_generation,
ATTR_ENERGY_CONSUMPTION: self.status.energy_consumption,
ATTR_POWER_CONSUMPTION: self.status.power_consumption,
ATTR_EFFICIENCY: self.status.normalized_ouput,
ATTR_TEMPERATURE: self.status.temperature,
ATTR_VOLTAGE: self.status.voltage,
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,
}
async def async_update(self) -> None:
"""Get the latest data from the PVOutput API and updates the state."""
self.status = await self.pvoutput.status()
@@ -0,0 +1,17 @@
{
"config": {
"step": {
"user": {
"description": "To authenticate with PVOutput you'll need to get the API key at {account_url}.\n\nThe system IDs of registered systems are listed on that same page.",
"data": {
"system_id": "System ID",
"api_key": "[%key:common::config_flow::data::api_key%]"
}
}
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
}
}
}
@@ -0,0 +1,17 @@
{
"config": {
"error": {
"cannot_connect": "Failed to connect",
"invalid_auth": "Invalid authentication"
},
"step": {
"user": {
"data": {
"api_key": "API Key",
"system_id": "System ID"
},
"description": "To authenticate with PVOutput you'll need to get the API key at {account_url}.\n\nThe system IDs of registered systems are listed on that same page."
}
}
}
}