Skip to content

Commit

Permalink
Merge pull request #17 from SaSa1983/master
Browse files Browse the repository at this point in the history
Fix PV3 Support, Allow 1, 2 or 3 strings
  • Loading branch information
ITTV-tools committed Feb 24, 2021
2 parents 6761c67 + 6dd7a41 commit a53af82
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 53 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ __pycache__/
# C extensions
*.so

# IntelliJ
.idea/
*.iml

# MacOs
.DS_Store

# Distribution / packaging
.Python
build/
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sensor:
- platform: kostal_plenticore
host: <IP>
password: <Password>
DC_Inputs: 2 #Optional Default 2 (2,3)
dc_inputs: 2 # Optional, default: 2, valid values: 1, 2, 3
monitored_conditions:
- BatteryPercent
- BatteryCycles
Expand Down Expand Up @@ -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
48 changes: 27 additions & 21 deletions custom_components/kostal_plenticore/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
POWER_WATT,
)

CONF_DCINPUTS = "dc_inputs"
CONF_DCINPUTS_DEFAULT = 2

SENSOR_TYPES = {
"BatteryPercent": [
"devices:local:battery",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
]
}
79 changes: 48 additions & 31 deletions custom_components/kostal_plenticore/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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})]
),
}
)
Expand All @@ -40,30 +39,47 @@ 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:
con = kostalplenticore.connect(host, password)
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],
)
]
Expand All @@ -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"
Expand Down

0 comments on commit a53af82

Please sign in to comment.