Handle offline state of SAJ inverters (fixes #29007) (#29009)

* Fix for SAJ issue #29007

* Sort mapping
This commit is contained in:
fredericvl 2019-11-25 14:46:49 +01:00 committed by Charles Garwood
parent 335473cb71
commit 3e4f7fddf2

View File

@ -24,6 +24,7 @@ from homeassistant.const import (
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
) )
from homeassistant.core import CALLBACK_TYPE, callback from homeassistant.core import CALLBACK_TYPE, callback
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_call_later from homeassistant.helpers.event import async_call_later
@ -38,12 +39,12 @@ UNIT_OF_MEASUREMENT_HOURS = "h"
INVERTER_TYPES = ["ethernet", "wifi"] INVERTER_TYPES = ["ethernet", "wifi"]
SAJ_UNIT_MAPPINGS = { SAJ_UNIT_MAPPINGS = {
"W": POWER_WATT, "": None,
"kWh": ENERGY_KILO_WATT_HOUR,
"h": UNIT_OF_MEASUREMENT_HOURS, "h": UNIT_OF_MEASUREMENT_HOURS,
"kg": MASS_KILOGRAMS, "kg": MASS_KILOGRAMS,
"kWh": ENERGY_KILO_WATT_HOUR,
"W": POWER_WATT,
"°C": TEMP_CELSIUS, "°C": TEMP_CELSIUS,
"": None,
} }
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -58,7 +59,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up SAJ sensors.""" """Set up the SAJ sensors."""
remove_interval_update = None remove_interval_update = None
wifi = config[CONF_TYPE] == INVERTER_TYPES[1] wifi = config[CONF_TYPE] == INVERTER_TYPES[1]
@ -80,7 +81,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
saj = pysaj.SAJ(config[CONF_HOST], **kwargs) saj = pysaj.SAJ(config[CONF_HOST], **kwargs)
done = await saj.read(sensor_def) done = await saj.read(sensor_def)
except pysaj.UnauthorizedException: except pysaj.UnauthorizedException:
_LOGGER.error("Username and/or password is wrong.") _LOGGER.error("Username and/or password is wrong")
return return
except pysaj.UnexpectedResponseException as err: except pysaj.UnexpectedResponseException as err:
_LOGGER.error( _LOGGER.error(
@ -88,13 +89,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
) )
return return
if done: if not done:
for sensor in sensor_def: raise PlatformNotReady
hass_sensors.append(
SAJsensor(saj.serialnumber, sensor, inverter_name=config.get(CONF_NAME))
)
async_add_entities(hass_sensors) for sensor in sensor_def:
hass_sensors.append(
SAJsensor(saj.serialnumber, sensor, inverter_name=config.get(CONF_NAME))
)
async_add_entities(hass_sensors)
async def async_saj(): async def async_saj():
"""Update all the SAJ sensors.""" """Update all the SAJ sensors."""
@ -167,7 +170,7 @@ class SAJsensor(Entity):
"""Representation of a SAJ sensor.""" """Representation of a SAJ sensor."""
def __init__(self, serialnumber, pysaj_sensor, inverter_name=None): def __init__(self, serialnumber, pysaj_sensor, inverter_name=None):
"""Initialize the sensor.""" """Initialize the SAJ sensor."""
self._sensor = pysaj_sensor self._sensor = pysaj_sensor
self._inverter_name = inverter_name self._inverter_name = inverter_name
self._serialnumber = serialnumber self._serialnumber = serialnumber