From 6dd7a41680e34633258dad3892e9bedf112868ae Mon Sep 17 00:00:00 2001 From: Sascha Sander Date: Wed, 24 Feb 2021 10:28:42 +0100 Subject: [PATCH] Fix PV3 Support, Allow 1, 2 or 3 strings --- .gitignore | 7 ++ README.md | 5 +- custom_components/kostal_plenticore/const.py | 48 ++++++----- custom_components/kostal_plenticore/sensor.py | 79 +++++++++++-------- 4 files changed, 86 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index b6e4761..2db7a54 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,13 @@ __pycache__/ # C extensions *.so +# IntelliJ +.idea/ +*.iml + +# MacOs +.DS_Store + # Distribution / packaging .Python build/ diff --git a/README.md b/README.md index 7c65650..d47d012 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ sensor: - platform: kostal_plenticore host: password: - DC_Inputs: 2 #Optional Default 2 (2,3) + dc_inputs: 2 # Optional, default: 2, valid values: 1, 2, 3 monitored_conditions: - BatteryPercent - BatteryCycles @@ -92,3 +92,6 @@ sensor: - LimitEvuAbs ``` +Note: +* PV2* sensors will be ignored if dc_inputs < 2 +* PV3* sensors will be ignored if dc_inputs < 3 diff --git a/custom_components/kostal_plenticore/const.py b/custom_components/kostal_plenticore/const.py index 4732927..1503d21 100644 --- a/custom_components/kostal_plenticore/const.py +++ b/custom_components/kostal_plenticore/const.py @@ -12,6 +12,9 @@ POWER_WATT, ) +CONF_DCINPUTS = "dc_inputs" +CONF_DCINPUTS_DEFAULT = 2 + SENSOR_TYPES = { "BatteryPercent": [ "devices:local:battery", @@ -90,27 +93,6 @@ ELECTRICAL_CURRENT_AMPERE, "mdi:solar-power", ], - "PV2Power": [ - "devices:local:pv2", - "P", - "Kostal PV2 Power", - POWER_WATT, - "mdi:solar-power", - ], - "PV2Voltage": [ - "devices:local:pv2", - "U", - "Kostal PV2 Voltage", - VOLT, - "mdi:solar-power", - ], - "PV2Current": [ - "devices:local:pv2", - "I", - "Kostal PV2 Current", - ELECTRICAL_CURRENT_AMPERE, - "mdi:solar-power", - ], "PVPower": [ "pvcombined", "P", @@ -479,3 +461,27 @@ "mdi:solar-power", ] } + +SENSORS_DC2 = { + "PV2Power": [ + "devices:local:pv2", + "P", + "Kostal PV2 Power", + POWER_WATT, + "mdi:solar-power", + ], + "PV2Voltage": [ + "devices:local:pv2", + "U", + "Kostal PV2 Voltage", + VOLT, + "mdi:solar-power", + ], + "PV2Current": [ + "devices:local:pv2", + "I", + "Kostal PV2 Current", + ELECTRICAL_CURRENT_AMPERE, + "mdi:solar-power", + ] +} diff --git a/custom_components/kostal_plenticore/sensor.py b/custom_components/kostal_plenticore/sensor.py index 530fafe..29d893e 100644 --- a/custom_components/kostal_plenticore/sensor.py +++ b/custom_components/kostal_plenticore/sensor.py @@ -7,7 +7,7 @@ import json from homeassistant.components.sensor import PLATFORM_SCHEMA -from .const import SENSOR_TYPES, SENSORS_DC3 +from .const import CONF_DCINPUTS, CONF_DCINPUTS_DEFAULT, SENSOR_TYPES, SENSORS_DC2, SENSORS_DC3 import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity @@ -21,15 +21,14 @@ """Platform for sensor integration.""" _LOGGER = logging.getLogger(__name__) -CONF_DCINPUTS = "DC_Inputs" -CONF_DCINPUTS_Default = 2 + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_HOST): cv.string, vol.Required(CONF_PASSWORD): cv.string, - vol.Optional(CONF_DCINPUTS, default=CONF_DCINPUTS_Default): vol.In([2, 3]), + vol.Optional(CONF_DCINPUTS, default=CONF_DCINPUTS_DEFAULT): vol.In([1, 2, 3]), vol.Optional(CONF_MONITORED_CONDITIONS, default=[]): vol.All( - cv.ensure_list, [vol.In({**SENSOR_TYPES, **SENSORS_DC3})] + cv.ensure_list, [vol.In({**SENSOR_TYPES, **SENSORS_DC2, **SENSORS_DC3})] ), } ) @@ -40,7 +39,6 @@ def setup_platform(hass, config, add_entities, discovery_info=None): host = config[CONF_HOST] password = config[CONF_PASSWORD] monitoredcondition = config[CONF_MONITORED_CONDITIONS] - #_LOGGER.error(config[CONF_DCINPUTS]) """ Login to Kostal Plenticore """ try: @@ -48,22 +46,40 @@ def setup_platform(hass, config, add_entities, discovery_info=None): con.login() except: _LOGGER.error('Could not connect to kostal plenticore, please restart HA') + + """ All sensors """ + allSensors = {**SENSOR_TYPES, **SENSORS_DC2, **SENSORS_DC3} + + """ If there're no monitored conditions use all possible values """ if len(monitoredcondition) == 0: - if(config[CONF_DCINPUTS] == 3): - monitoredcondition = {**monitoredcondition, **SENSORS_DC3} + if(config[CONF_DCINPUTS] == 2): + monitoredcondition = {**SENSOR_TYPES, **SENSORS_DC2} + elif(config[CONF_DCINPUTS] == 3): + monitoredcondition = allSensors else: monitoredcondition = SENSOR_TYPES.keys() + else: + """ Remove keys that do not match dccount """ + if(config[CONF_DCINPUTS] < 3): + for dc3sensorKey in SENSORS_DC3.keys(): + _LOGGER.warning('Ignoring monitored condition %s due to dc_inputs: %d', dc3sensorKey, config[CONF_DCINPUTS]) + monitoredcondition.remove(dc3sensorKey) + if(config[CONF_DCINPUTS] < 2): + for dc2sensorKey in SENSORS_DC2.keys(): + _LOGGER.warning('Ignoring monitored condition %s due to dc_inputs: %d', dc2sensorKey, config[CONF_DCINPUTS]) + monitoredcondition.remove(dc2sensorKey) + for sensor in monitoredcondition: add_entities( [ plenticore( con, - SENSOR_TYPES[sensor][2], - SENSOR_TYPES[sensor][0], - SENSOR_TYPES[sensor][1], - SENSOR_TYPES[sensor][3], - SENSOR_TYPES[sensor][4], + allSensors[sensor][2], + allSensors[sensor][0], + allSensors[sensor][1], + allSensors[sensor][3], + allSensors[sensor][4], config[CONF_DCINPUTS], ) ] @@ -82,28 +98,29 @@ def getData(self): ) except: pv1 = "error" + try: - pv2 = int( - self.api.getProcessdata("devices:local:pv2", [self.id])[0]["value"] - ) + pv2 = 0 + if(self.dccount > 1): + pv2 = int( + self.api.getProcessdata("devices:local:pv2", [self.id])[0]["value"] + ) except: pv2 = "error" - if(self.dccount == 3): - try: - pv3 = int( - self.api.getProcessdata("devices:local:pv3", [self.id])[0]["value"] - ) - except: - pv3 = "error" - if(pv1 != "error" and pv2 != "error" and pv3 != "error"): - value = pv1 + pv2 + pv3 - else: - value= "error" + + try: + pv3 = 0 + if(self.dccount > 2): + pv3 = int( + self.api.getProcessdata("devices:local:pv3", [self.id])[0]["value"] + ) + except: + pv3 = "error" + + if(pv1 != "error" and pv2 != "error" and pv3 != "error"): + value = pv1 + pv2 + pv3 else: - if(pv1 != "error" and pv2 != "error"): - value = pv1 + pv2 - else: - value= "error" + value= "error" elif ( self.id == "Frequency"