2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Enphase Envoy solar energy monitor."""
|
2020-12-28 22:58:09 +00:00
|
|
|
|
|
|
|
from datetime import timedelta
|
2018-08-02 21:14:43 +00:00
|
|
|
import logging
|
|
|
|
|
2020-12-28 22:58:09 +00:00
|
|
|
import async_timeout
|
2019-12-05 05:21:25 +00:00
|
|
|
from envoy_reader.envoy_reader import EnvoyReader
|
2020-12-28 22:58:09 +00:00
|
|
|
import httpx
|
2018-08-02 21:14:43 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2019-03-02 10:29:59 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_IP_ADDRESS,
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
2019-09-17 18:24:03 +00:00
|
|
|
CONF_NAME,
|
2019-12-18 00:51:19 +00:00
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
2019-07-31 19:25:30 +00:00
|
|
|
ENERGY_WATT_HOUR,
|
2019-12-05 05:21:25 +00:00
|
|
|
POWER_WATT,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-12-28 22:58:09 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2019-12-05 05:21:25 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2020-12-28 22:58:09 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
UpdateFailed,
|
|
|
|
)
|
2018-08-02 21:14:43 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
SENSORS = {
|
2019-03-02 10:29:59 +00:00
|
|
|
"production": ("Envoy Current Energy Production", POWER_WATT),
|
2019-07-08 14:21:08 +00:00
|
|
|
"daily_production": ("Envoy Today's Energy Production", ENERGY_WATT_HOUR),
|
2019-07-31 19:25:30 +00:00
|
|
|
"seven_days_production": (
|
|
|
|
"Envoy Last Seven Days Energy Production",
|
|
|
|
ENERGY_WATT_HOUR,
|
|
|
|
),
|
|
|
|
"lifetime_production": ("Envoy Lifetime Energy Production", ENERGY_WATT_HOUR),
|
2019-07-08 14:21:08 +00:00
|
|
|
"consumption": ("Envoy Current Energy Consumption", POWER_WATT),
|
2019-07-31 19:25:30 +00:00
|
|
|
"daily_consumption": ("Envoy Today's Energy Consumption", ENERGY_WATT_HOUR),
|
|
|
|
"seven_days_consumption": (
|
|
|
|
"Envoy Last Seven Days Energy Consumption",
|
|
|
|
ENERGY_WATT_HOUR,
|
|
|
|
),
|
|
|
|
"lifetime_consumption": ("Envoy Lifetime Energy Consumption", ENERGY_WATT_HOUR),
|
|
|
|
"inverters": ("Envoy Inverter", POWER_WATT),
|
|
|
|
}
|
2018-08-02 21:14:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ICON = "mdi:flash"
|
2018-09-01 21:45:47 +00:00
|
|
|
CONST_DEFAULT_HOST = "envoy"
|
2018-08-02 21:14:43 +00:00
|
|
|
|
2020-12-28 22:58:09 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=60)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_IP_ADDRESS, default=CONST_DEFAULT_HOST): cv.string,
|
2019-12-18 00:51:19 +00:00
|
|
|
vol.Optional(CONF_USERNAME, default="envoy"): cv.string,
|
|
|
|
vol.Optional(CONF_PASSWORD, default=""): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSORS)): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(list(SENSORS))]
|
|
|
|
),
|
2019-09-17 18:24:03 +00:00
|
|
|
vol.Optional(CONF_NAME, default=""): cv.string,
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|
2018-08-02 21:14:43 +00:00
|
|
|
|
|
|
|
|
2020-12-28 22:58:09 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
homeassistant, config, async_add_entities, discovery_info=None
|
|
|
|
):
|
2018-08-02 21:14:43 +00:00
|
|
|
"""Set up the Enphase Envoy sensor."""
|
|
|
|
ip_address = config[CONF_IP_ADDRESS]
|
|
|
|
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
2019-09-17 18:24:03 +00:00
|
|
|
name = config[CONF_NAME]
|
2019-12-18 00:51:19 +00:00
|
|
|
username = config[CONF_USERNAME]
|
|
|
|
password = config[CONF_PASSWORD]
|
|
|
|
|
2020-12-28 22:58:09 +00:00
|
|
|
if "inverters" in monitored_conditions:
|
|
|
|
envoy_reader = EnvoyReader(ip_address, username, password, inverters=True)
|
|
|
|
else:
|
|
|
|
envoy_reader = EnvoyReader(ip_address, username, password)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await envoy_reader.getData()
|
|
|
|
except httpx.HTTPStatusError as err:
|
|
|
|
_LOGGER.error("Authentication failure during setup: %s", err)
|
|
|
|
return
|
|
|
|
except httpx.HTTPError as err:
|
|
|
|
raise PlatformNotReady from err
|
|
|
|
|
|
|
|
async def async_update_data():
|
|
|
|
"""Fetch data from API endpoint."""
|
|
|
|
data = {}
|
|
|
|
async with async_timeout.timeout(30):
|
|
|
|
try:
|
|
|
|
await envoy_reader.getData()
|
|
|
|
except httpx.HTTPError as err:
|
|
|
|
raise UpdateFailed(f"Error communicating with API: {err}") from err
|
|
|
|
|
|
|
|
for condition in monitored_conditions:
|
|
|
|
if condition != "inverters":
|
|
|
|
data[condition] = await getattr(envoy_reader, condition)()
|
|
|
|
else:
|
|
|
|
data["inverters_production"] = await getattr(
|
|
|
|
envoy_reader, "inverters_production"
|
|
|
|
)()
|
|
|
|
|
|
|
|
_LOGGER.debug("Retrieved data from API: %s", data)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
coordinator = DataUpdateCoordinator(
|
|
|
|
homeassistant,
|
|
|
|
_LOGGER,
|
|
|
|
name="sensor",
|
|
|
|
update_method=async_update_data,
|
|
|
|
update_interval=SCAN_INTERVAL,
|
|
|
|
)
|
|
|
|
|
|
|
|
await coordinator.async_refresh()
|
|
|
|
|
|
|
|
if coordinator.data is None:
|
|
|
|
raise PlatformNotReady
|
2018-08-02 21:14:43 +00:00
|
|
|
|
2019-07-08 14:21:08 +00:00
|
|
|
entities = []
|
2018-08-02 21:14:43 +00:00
|
|
|
for condition in monitored_conditions:
|
2020-12-28 22:58:09 +00:00
|
|
|
entity_name = ""
|
|
|
|
if (
|
|
|
|
condition == "inverters"
|
|
|
|
and coordinator.data.get("inverters_production") is not None
|
|
|
|
):
|
|
|
|
for inverter in coordinator.data["inverters_production"]:
|
|
|
|
entity_name = f"{name}{SENSORS[condition][0]} {inverter}"
|
|
|
|
split_name = entity_name.split(" ")
|
|
|
|
serial_number = split_name[-1]
|
|
|
|
entities.append(
|
|
|
|
Envoy(
|
|
|
|
condition,
|
|
|
|
entity_name,
|
|
|
|
serial_number,
|
|
|
|
SENSORS[condition][1],
|
|
|
|
coordinator,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-12-28 22:58:09 +00:00
|
|
|
)
|
|
|
|
elif condition != "inverters":
|
|
|
|
entity_name = f"{name}{SENSORS[condition][0]}"
|
2019-07-31 19:25:30 +00:00
|
|
|
entities.append(
|
|
|
|
Envoy(
|
2019-09-17 18:24:03 +00:00
|
|
|
condition,
|
2020-12-28 22:58:09 +00:00
|
|
|
entity_name,
|
|
|
|
None,
|
2019-09-17 18:24:03 +00:00
|
|
|
SENSORS[condition][1],
|
2020-12-28 22:58:09 +00:00
|
|
|
coordinator,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
|
|
|
)
|
2020-12-28 22:58:09 +00:00
|
|
|
|
2019-07-08 14:21:08 +00:00
|
|
|
async_add_entities(entities)
|
2018-08-02 21:14:43 +00:00
|
|
|
|
|
|
|
|
2020-12-28 22:58:09 +00:00
|
|
|
class Envoy(CoordinatorEntity):
|
|
|
|
"""Envoy entity."""
|
2018-08-02 21:14:43 +00:00
|
|
|
|
2020-12-28 22:58:09 +00:00
|
|
|
def __init__(self, sensor_type, name, serial_number, unit, coordinator):
|
|
|
|
"""Initialize Envoy entity."""
|
2019-12-18 00:51:19 +00:00
|
|
|
self._type = sensor_type
|
2018-08-02 21:14:43 +00:00
|
|
|
self._name = name
|
2020-12-28 22:58:09 +00:00
|
|
|
self._serial_number = serial_number
|
2018-08-02 21:14:43 +00:00
|
|
|
self._unit_of_measurement = unit
|
2020-12-28 22:58:09 +00:00
|
|
|
|
|
|
|
super().__init__(coordinator)
|
2018-08-02 21:14:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
2020-12-28 22:58:09 +00:00
|
|
|
if self._type != "inverters":
|
|
|
|
value = self.coordinator.data.get(self._type)
|
|
|
|
|
|
|
|
elif (
|
|
|
|
self._type == "inverters"
|
|
|
|
and self.coordinator.data.get("inverters_production") is not None
|
|
|
|
):
|
|
|
|
value = self.coordinator.data.get("inverters_production").get(
|
|
|
|
self._serial_number
|
|
|
|
)[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return value
|
2018-08-02 21:14:43 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
|
|
|
return self._unit_of_measurement
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return ICON
|
|
|
|
|
2019-12-18 00:51:19 +00:00
|
|
|
@property
|
2021-03-11 15:51:03 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-12-18 00:51:19 +00:00
|
|
|
"""Return the state attributes."""
|
2020-12-28 22:58:09 +00:00
|
|
|
if (
|
|
|
|
self._type == "inverters"
|
|
|
|
and self.coordinator.data.get("inverters_production") is not None
|
|
|
|
):
|
|
|
|
value = self.coordinator.data.get("inverters_production").get(
|
|
|
|
self._serial_number
|
|
|
|
)[1]
|
|
|
|
return {"last_reported": value}
|
2019-12-18 00:51:19 +00:00
|
|
|
|
|
|
|
return None
|