From f90fe7e628498fa3ed0193896c6893ebdf71f8b5 Mon Sep 17 00:00:00 2001 From: Jesse Rizzo <32472573+jesserizzo@users.noreply.github.com> Date: Mon, 8 Jul 2019 09:21:08 -0500 Subject: [PATCH] Enphase envoy individual inverter production (#24445) * bump envoy_reader version to 0.4 * bump dependency envoy_reader to 0.4 * Enphase envoy get individual inverter production * Add period in function description * Fix dumb typo * Define _attributes in __init__ * Better error messages, make update async * Fix format error * Fix pylint errors * set unknown state to None * Bump envoy_reader version to 0.8 * Change attributes to separate sensors * Fix dumb thing * Improve platform_setup for inverters * Remove unneeded self._attributes, refactor platform setup * Refactor platform setup --- .../components/enphase_envoy/manifest.json | 2 +- .../components/enphase_envoy/sensor.py | 62 ++++++++++++++----- requirements_all.txt | 2 +- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/enphase_envoy/manifest.json b/homeassistant/components/enphase_envoy/manifest.json index 1a816bc91d9..60f252c59a6 100644 --- a/homeassistant/components/enphase_envoy/manifest.json +++ b/homeassistant/components/enphase_envoy/manifest.json @@ -3,7 +3,7 @@ "name": "Enphase envoy", "documentation": "https://www.home-assistant.io/components/enphase_envoy", "requirements": [ - "envoy_reader==0.4" + "envoy_reader==0.8" ], "dependencies": [], "codeowners": [] diff --git a/homeassistant/components/enphase_envoy/sensor.py b/homeassistant/components/enphase_envoy/sensor.py index 7077e12d750..b859313a41e 100644 --- a/homeassistant/components/enphase_envoy/sensor.py +++ b/homeassistant/components/enphase_envoy/sensor.py @@ -7,21 +7,26 @@ from homeassistant.helpers.entity import Entity from homeassistant.components.sensor import PLATFORM_SCHEMA import homeassistant.helpers.config_validation as cv from homeassistant.const import ( - CONF_IP_ADDRESS, CONF_MONITORED_CONDITIONS, POWER_WATT) + CONF_IP_ADDRESS, CONF_MONITORED_CONDITIONS, POWER_WATT, ENERGY_WATT_HOUR) _LOGGER = logging.getLogger(__name__) SENSORS = { "production": ("Envoy Current Energy Production", POWER_WATT), - "daily_production": ("Envoy Today's Energy Production", "Wh"), - "seven_days_production": ("Envoy Last Seven Days Energy Production", "Wh"), - "lifetime_production": ("Envoy Lifetime Energy Production", "Wh"), - "consumption": ("Envoy Current Energy Consumption", "W"), - "daily_consumption": ("Envoy Today's Energy Consumption", "Wh"), + "daily_production": ("Envoy Today's Energy Production", ENERGY_WATT_HOUR), + "seven_days_production": ("Envoy Last Seven Days Energy Production", + ENERGY_WATT_HOUR), + "lifetime_production": ("Envoy Lifetime Energy Production", + ENERGY_WATT_HOUR), + "consumption": ("Envoy Current Energy Consumption", POWER_WATT), + "daily_consumption": ("Envoy Today's Energy Consumption", + ENERGY_WATT_HOUR), "seven_days_consumption": ("Envoy Last Seven Days Energy Consumption", - "Wh"), - "lifetime_consumption": ("Envoy Lifetime Energy Consumption", "Wh") + ENERGY_WATT_HOUR), + "lifetime_consumption": ("Envoy Lifetime Energy Consumption", + ENERGY_WATT_HOUR), + "inverters": ("Envoy Inverter", POWER_WATT) } @@ -34,15 +39,29 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.All(cv.ensure_list, [vol.In(list(SENSORS))])}) -def setup_platform(hass, config, add_entities, discovery_info=None): +async def async_setup_platform(hass, config, async_add_entities, + discovery_info=None): """Set up the Enphase Envoy sensor.""" + from envoy_reader.envoy_reader import EnvoyReader + ip_address = config[CONF_IP_ADDRESS] monitored_conditions = config[CONF_MONITORED_CONDITIONS] + entities = [] # Iterate through the list of sensors for condition in monitored_conditions: - add_entities([Envoy(ip_address, condition, SENSORS[condition][0], - SENSORS[condition][1])], True) + if condition == "inverters": + inverters = await EnvoyReader(ip_address).inverters_production() + if isinstance(inverters, dict): + for inverter in inverters: + entities.append(Envoy(ip_address, condition, + "{} {}".format(SENSORS[condition][0], + inverter), + SENSORS[condition][1])) + else: + entities.append(Envoy(ip_address, condition, SENSORS[condition][0], + SENSORS[condition][1])) + async_add_entities(entities) class Envoy(Entity): @@ -76,8 +95,23 @@ class Envoy(Entity): """Icon to use in the frontend, if any.""" return ICON - def update(self): + async def async_update(self): """Get the energy production data from the Enphase Envoy.""" - from envoy_reader import EnvoyReader + from envoy_reader.envoy_reader import EnvoyReader - self._state = getattr(EnvoyReader(self._ip_address), self._type)() + if self._type != "inverters": + _state = await getattr(EnvoyReader(self._ip_address), self._type)() + if isinstance(_state, int): + self._state = _state + else: + _LOGGER.error(_state) + self._state = None + + elif self._type == "inverters": + inverters = await (EnvoyReader(self._ip_address) + .inverters_production()) + if isinstance(inverters, dict): + serial_number = self._name.split(" ")[2] + self._state = inverters[serial_number] + else: + self._state = None diff --git a/requirements_all.txt b/requirements_all.txt index e8f054b3a8d..fcfa3173632 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -424,7 +424,7 @@ env_canada==0.0.10 # envirophat==0.0.6 # homeassistant.components.enphase_envoy -envoy_reader==0.4 +envoy_reader==0.8 # homeassistant.components.season ephem==3.7.6.0