Skip to content

Instantly share code, notes, and snippets.

@pve84
Created August 29, 2018 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pve84/fd19887f824752bd9ee7101bca0e1ab2 to your computer and use it in GitHub Desktop.
Save pve84/fd19887f824752bd9ee7101bca0e1ab2 to your computer and use it in GitHub Desktop.
"""
configuration.yaml
sensor:
- platform: zeversolar_inverter
host: IP_ADDRESS
port: 80
scan_interval: 10
resources:
- elecsolar
- elecsolartoday
"""
import logging
from datetime import timedelta
import requests
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
import homeassistant.helpers.config_validation as cv
from homeassistant.const import (
CONF_HOST, CONF_PORT, CONF_SCAN_INTERVAL, CONF_RESOURCES)
from homeassistant.util import Throttle
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
BASE_URL = 'http://{0}:{1}{2}'
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=10)
SENSOR_PREFIX = 'PV '
SENSOR_TYPES = {
'elecsolar': ['Power Solar', 'Watt', 'mdi:weather-sunny'],
'elecsolartoday': ['Power Solar Generated Today', 'KWh', 'mdi:weather-sunny']
}
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_PORT, default=80): cv.positive_int,
vol.Required(CONF_RESOURCES, default=[]):
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
})
def setup_platform(hass, config, add_entities, discovery_info=None):
scan_interval = config.get(CONF_SCAN_INTERVAL)
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)
try:
data = ZeversolarData(host, port)
except requests.exceptions.HTTPError as error:
_LOGGER.error(error)
return False
entities = []
for resource in config[CONF_RESOURCES]:
sensor_type = resource.lower()
if sensor_type not in SENSOR_TYPES:
SENSOR_TYPES[sensor_type] = [
sensor_type.title(), '', 'mdi:flash']
entities.append(ZeversolarSensor(data, sensor_type))
add_entities(entities)
# pylint: disable=abstract-method
class ZeversolarData(object):
def __init__(self, host, port):
"""Initialize the inverter"""
self._host = host
self._port = port
self.data = None
@Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self):
"""Update the data from the inverter."""
try:
r = requests.get(BASE_URL.format(self._host, self._port,'/home.cgi'), timeout=5)
self.data = r.text
temp = r.text.splitlines()
_LOGGER.info("Data = %s",temp[10])
_LOGGER.info("Data = %s",temp[11])
except requests.exceptions.RequestException:
_LOGGER.error("Error occurred while fetching data.")
self.data = None
return False
class ZeversolarSensor(Entity):
def __init__(self, data, sensor_type):
"""Initialize the sensor."""
self.data = data
self.type = sensor_type
self._name = SENSOR_PREFIX + SENSOR_TYPES[self.type][0]
self._unit = SENSOR_TYPES[self.type][1]
self._icon = SENSOR_TYPES[self.type][2]
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return self._icon
@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
def update(self):
"""Get the latest data and use it to update our sensor state."""
self.data.update()
energy = str(self.data.data).splitlines()
"""Go to http://inverter.ip:port/home.cgi"""
if self.type == 'elecsolar':
self._state = int(energy[10])
elif self.type == 'elecsolartoday':
self._state = float(energy[11])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment