2019-05-26 02:55:30 +00:00
|
|
|
"""Support for Solax inverter via local API."""
|
|
|
|
import asyncio
|
|
|
|
from datetime import timedelta
|
|
|
|
|
2019-09-19 18:52:15 +00:00
|
|
|
from solax import real_time_api
|
|
|
|
from solax.inverter import InverterError
|
2019-05-26 02:55:30 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2019-12-09 13:38:01 +00:00
|
|
|
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT, TEMP_CELSIUS
|
2019-05-26 02:55:30 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
2019-12-09 13:38:01 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-05-26 02:55:30 +00:00
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
|
|
|
|
2019-09-19 18:52:15 +00:00
|
|
|
DEFAULT_PORT = 80
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_IP_ADDRESS): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
}
|
|
|
|
)
|
2019-05-26 02:55:30 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2019-05-26 02:55:30 +00:00
|
|
|
"""Platform setup."""
|
2019-09-19 18:52:15 +00:00
|
|
|
api = await real_time_api(config[CONF_IP_ADDRESS], config[CONF_PORT])
|
2019-05-26 02:55:30 +00:00
|
|
|
endpoint = RealTimeDataEndpoint(hass, api)
|
2019-06-24 15:34:20 +00:00
|
|
|
resp = await api.get_data()
|
|
|
|
serial = resp.serial_number
|
2019-05-26 02:55:30 +00:00
|
|
|
hass.async_add_job(endpoint.async_refresh)
|
|
|
|
async_track_time_interval(hass, endpoint.async_refresh, SCAN_INTERVAL)
|
|
|
|
devices = []
|
2019-09-19 18:52:15 +00:00
|
|
|
for sensor, (idx, unit) in api.inverter.sensor_map().items():
|
2019-07-31 19:25:30 +00:00
|
|
|
if unit == "C":
|
2019-05-26 02:55:30 +00:00
|
|
|
unit = TEMP_CELSIUS
|
2019-09-03 19:14:39 +00:00
|
|
|
uid = f"{serial}-{idx}"
|
2019-06-24 15:34:20 +00:00
|
|
|
devices.append(Inverter(uid, serial, sensor, unit))
|
2019-05-26 02:55:30 +00:00
|
|
|
endpoint.sensors = devices
|
|
|
|
async_add_entities(devices)
|
|
|
|
|
|
|
|
|
|
|
|
class RealTimeDataEndpoint:
|
|
|
|
"""Representation of a Sensor."""
|
|
|
|
|
|
|
|
def __init__(self, hass, api):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
self.hass = hass
|
|
|
|
self.api = api
|
|
|
|
self.ready = asyncio.Event()
|
|
|
|
self.sensors = []
|
|
|
|
|
|
|
|
async def async_refresh(self, now=None):
|
|
|
|
"""Fetch new state data for the sensor.
|
|
|
|
|
|
|
|
This is the only method that should fetch new data for Home Assistant.
|
|
|
|
"""
|
|
|
|
try:
|
2019-06-23 09:16:39 +00:00
|
|
|
api_response = await self.api.get_data()
|
2019-05-26 02:55:30 +00:00
|
|
|
self.ready.set()
|
2020-08-28 11:50:32 +00:00
|
|
|
except InverterError as err:
|
2019-05-26 02:55:30 +00:00
|
|
|
if now is not None:
|
|
|
|
self.ready.clear()
|
2019-09-19 18:52:15 +00:00
|
|
|
return
|
2020-08-28 11:50:32 +00:00
|
|
|
raise PlatformNotReady from err
|
2019-06-23 09:16:39 +00:00
|
|
|
data = api_response.data
|
2019-05-26 02:55:30 +00:00
|
|
|
for sensor in self.sensors:
|
2019-06-23 09:16:39 +00:00
|
|
|
if sensor.key in data:
|
|
|
|
sensor.value = data[sensor.key]
|
2019-05-26 02:55:30 +00:00
|
|
|
sensor.async_schedule_update_ha_state()
|
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class Inverter(SensorEntity):
|
2019-05-26 02:55:30 +00:00
|
|
|
"""Class for a sensor."""
|
|
|
|
|
2019-06-24 15:34:20 +00:00
|
|
|
def __init__(self, uid, serial, key, unit):
|
2019-05-26 02:55:30 +00:00
|
|
|
"""Initialize an inverter sensor."""
|
2019-06-24 15:34:20 +00:00
|
|
|
self.uid = uid
|
|
|
|
self.serial = serial
|
2019-05-26 02:55:30 +00:00
|
|
|
self.key = key
|
|
|
|
self.value = None
|
|
|
|
self.unit = unit
|
|
|
|
|
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_value(self):
|
2019-05-26 02:55:30 +00:00
|
|
|
"""State of this inverter attribute."""
|
|
|
|
return self.value
|
|
|
|
|
2019-06-24 15:34:20 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return unique id."""
|
|
|
|
return self.uid
|
|
|
|
|
2019-05-26 02:55:30 +00:00
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Name of this inverter attribute."""
|
2019-09-03 19:14:39 +00:00
|
|
|
return f"Solax {self.serial} {self.key}"
|
2019-05-26 02:55:30 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_unit_of_measurement(self):
|
2019-05-26 02:55:30 +00:00
|
|
|
"""Return the unit of measurement."""
|
|
|
|
return self.unit
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed."""
|
|
|
|
return False
|