-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Add Enphase Envoy component #15081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Enphase Envoy component #15081
Changes from 8 commits
c7ca273
75c87fa
30ece48
e549f4e
4adfbde
66d0df8
ad17cb1
b8d3738
fe90ffd
a38ebe7
a325c8a
7d1df3a
77e5355
d43d1bc
d90bd63
8d88fa2
bf47d88
5004d3c
11cc650
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| """ | ||
| Support for monitoring energy usage and solar panel energy production. | ||
| using the Enphase Envoy. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| """ | ||
| import logging | ||
| import json | ||
| import voluptuous as vol | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a blank line between logging and voluptuous. |
||
| from homeassistant.helpers.entity import Entity | ||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| import homeassistant.helpers.config_validation as cv | ||
|
|
||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| CONF_IP_ADDRESS = 'ip' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please import and use |
||
| CONF_MONITORED_CONDITIONS = 'monitored_conditions' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import and use |
||
| DEFAULT_NAMES = [ | ||
| "Envoy Current Energy Production", | ||
| "Envoy Today's Energy Production", | ||
| "Envoy Last Seven Days Energy Production", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line under-indented for visual indent |
||
| "Envoy Lifetime Energy Production", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line under-indented for visual indent |
||
| "Envoy Current Energy Consumption", | ||
| "Envoy Today's Energy Consumption", | ||
| "Envoy Last Seven Days Energy Consumption", | ||
| "Envoy Lifetime Energy Consumption"] | ||
|
|
||
| SENSOR_TYPES = [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's cleaner to make this a list of lists or tuples, where the items in the list or tuple are the sensor type and the corresponding sensor name. Then we don't have to keep track of that the order in the list of types matches the order of the list of names. See eg: |
||
| "production", "daily_production", "7_days_production", | ||
| "lifetime_production", "consumption", "daily_consumption", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line under-indented for visual indent |
||
| "7_days_consumption", "lifetime_consumption"] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line under-indented for visual indent |
||
|
|
||
| ICON = 'mdi:flash' | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Required(CONF_IP_ADDRESS): cv.string, | ||
| vol.Optional(CONF_MONITORED_CONDITIONS): vol.All(cv.ensure_list) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sure the selected conditions are in the vol.Optional(CONF_MONITORED_CONDITIONS, default=SENSOR_TYPES): vol.All(
cv.ensure_list, [vol.In(SENSOR_TYPES)]) |
||
|
|
||
| }) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Set up the Enphase Envoy sensor.""" | ||
| ip_address = config.get(CONF_IP_ADDRESS) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use ip_address = config[CONF_IP_ADDRESS] |
||
| monitored_conditions = config.get(CONF_MONITORED_CONDITIONS, {}) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. monitored_conditions = config[CONF_MONITORED_CONDITIONS] |
||
|
|
||
| # Iterate through the list of sensors, adding it if either monitored | ||
| # conditions has been left out of the config, or that the given sensor | ||
| # is in the monitored conditions list | ||
| for i in range(8): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just iterate |
||
| if monitored_conditions == {} or \ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validation should already be done in the config schema, so we don't need to check that here. |
||
| SENSOR_TYPES[i] in monitored_conditions: | ||
| add_devices([Envoy(ip_address, DEFAULT_NAMES[i], | ||
| SENSOR_TYPES[i])], True) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line under-indented for visual indent |
||
|
|
||
|
|
||
| class Envoy(Entity): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too many blank lines (3) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 2 blank lines, found 1 |
||
| """Implementation of the Enphase Envoy sensors.""" | ||
|
|
||
| def __init__(self, ip_address, name, sensor_type): | ||
| """Initialize the sensor.""" | ||
| self._url = "http://{}/production.json".format(ip_address) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too many blank lines (2) |
||
| self._name = name | ||
| if sensor_type == 'production' or sensor_type == 'consumption': | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too many blank lines (2)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move the unit of measurement type to the nested list of sensor types. If no unit should be used, put |
||
| self._unit_of_measurement = 'W' | ||
| else: | ||
| self._unit_of_measurement = "Wh" | ||
|
|
||
| self._type = sensor_type | ||
| self._state = None | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the sensor.""" | ||
| return self._state | ||
|
|
||
| @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 | ||
|
|
||
| def update(self): | ||
| """Get the energy production data from the Enphase Envoy.""" | ||
| import requests | ||
| try: | ||
| response = requests.get(self._url, timeout=5) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the RestData class (components/sensor/rest.py) here? May save a bit of code duplication. |
||
| except (requests.exceptions.RequestException, ValueError): | ||
| _LOGGER.warning( | ||
| 'Could not update status for Enphase Envoy (%s)', | ||
| self._name) | ||
| return | ||
|
|
||
| if response.status_code != 200: | ||
| _LOGGER.warning( | ||
| 'Invalid status_code from Enphase Envoy: %s (%s)', | ||
| response.status_code, self._name) | ||
| return | ||
|
|
||
| response_parsed = json.loads(response.text) | ||
| if self._type == "production": | ||
| self._state = int(response_parsed["production"][1]["wNow"]) | ||
| elif self._type == "daily_production": | ||
| self._state = int(response_parsed["production"][1]["whToday"]) | ||
| elif self._type == "7_days_production": | ||
| self._state = \ | ||
| int(response_parsed["production"][1]["whLastSevenDays"]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line missing indentation or outdented |
||
| elif self._type == "lifetime_production": | ||
| self._state = int(response_parsed["production"][1]["whLifetime"]) | ||
|
|
||
| elif self._type == "consumption": | ||
| self._state = int(response_parsed["consumption"][0]["wNow"]) | ||
| elif self._type == "daily_consumption": | ||
| self._state = int(response_parsed["consumption"][0]["whToday"]) | ||
| elif self._type == "7_days_consumption": | ||
| self._state = \ | ||
| int(response_parsed["consumption"][0]["whLastSevenDays"]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continuation line missing indentation or outdented |
||
| elif self._type == "lifetime_consumption": | ||
| self._state = int(response_parsed["consumption"][0]["whLifetime"]) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a blank line between standard library and 3rd party imports and 3rd party and homeassistant imports.