Skip to content
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions homeassistant/components/sensor/enphase_envoy.py
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

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please import and use CONF_IP_ADDRESS from const.py.

CONF_MONITORED_CONDITIONS = 'monitored_conditions'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import and use CONF_MONITORED_CONDITIONS from const.py.

DEFAULT_NAMES = [
"Envoy Current Energy Production",
"Envoy Today's Energy Production",
"Envoy Last Seven Days Energy Production",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent

"Envoy Lifetime Energy Production",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 = [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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:
https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/sensor/airvisual.py#L43-L47

"production", "daily_production", "7_days_production",
"lifetime_production", "consumption", "daily_consumption",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent

"7_days_consumption", "lifetime_consumption"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure the selected conditions are in the SENSOR_TYPES list and default to the whole list.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use dict.get on required config keys or keys that have default values.

ip_address = config[CONF_IP_ADDRESS]

monitored_conditions = config.get(CONF_MONITORED_CONDITIONS, {})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just iterate monitored_conditions.

if monitored_conditions == {} or \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

continuation line under-indented for visual indent



class Envoy(Entity):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too many blank lines (3)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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':

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too many blank lines (2)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 None in the list.

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)

@exxamalte exxamalte Jun 22, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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.
Or maybe even use one instance of RestData and share across all sensors?

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"])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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"])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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"])