Skip to content

Commit

Permalink
Add price-based charging switch
Browse files Browse the repository at this point in the history
  • Loading branch information
zusorio committed Feb 23, 2023
1 parent f351ea2 commit cbab475
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
15 changes: 15 additions & 0 deletions custom_components/openwbmqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ def fun_change_charge_current_per_cp(call):

payload = str(call.data.get("target_current"))
hass.components.mqtt.publish(hass, topic, payload)

def fun_enable_disable_price_based_charging(call):
"""Enable or disable price-based charging for charge point # --> set/lp#/etBasedCharging [0,1]."""
topic = f"{call.data.get('mqtt_prefix')}/set/lp{call.data.get('charge_point_id')}/etBasedCharging"
_LOGGER.debug("topic (enable_disable_price_based_charging): %s", topic)

if call.data.get("selected_status") == "On":
payload = str(1)
hass.components.mqtt.publish(hass, topic, payload)

else:
payload = str(0)
hass.components.mqtt.publish(hass, topic, payload)

# Register our services with Home Assistant.
hass.services.async_register(DOMAIN, "enable_disable_cp", fun_enable_disable_cp)
Expand All @@ -96,6 +109,7 @@ def fun_change_charge_current_per_cp(call):
hass.services.async_register(
DOMAIN, "change_charge_current_per_cp", fun_change_charge_current_per_cp
)
hass.services.async_register(DOMAIN, "enable_disable_price_based_charging", fun_enable_disable_price_based_charging)

# Return boolean to indicate that initialization was successfully.
return True
Expand All @@ -108,6 +122,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.services.async_remove(DOMAIN, "change_global_charge_mode")
hass.services.async_remove(DOMAIN, "change_charge_limitation_per_cp")
hass.services.async_remove(DOMAIN, "change_charge_current_per_cp")
hass.services.async_remove(DOMAIN, "enable_disable_price_based_charging")
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

return unload_ok
10 changes: 10 additions & 0 deletions custom_components/openwbmqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class openwbSwitchEntityDescription(SwitchEntityDescription):

mqttTopicCommand: str | None = None
mqttTopicCurrentValue: str | None = None
mqttTopicChargeMode: str | None = None


@dataclass
Expand Down Expand Up @@ -714,6 +715,15 @@ class openWBNumberEntityDescription(NumberEntityDescription):
mqttTopicCurrentValue="ChargePointEnabled",
device_class=SwitchDeviceClass.SWITCH,
),
openwbSwitchEntityDescription(
key="PriceBasedCharging",
entity_category=EntityCategory.CONFIG,
name="Preisbasiertes Laden (Modus Sofortladen)",
device_class=SwitchDeviceClass.SWITCH,
mqttTopicCommand="etBasedCharging",
mqttTopicCurrentValue="etBasedCharging",
mqttTopicChargeMode="sofort",
),
]

NUMBERS_GLOBAL = [
Expand Down
30 changes: 30 additions & 0 deletions custom_components/openwbmqtt/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,33 @@ change_charge_current_per_cp:
min: 6
max: 16
step: 1
enable_disable_price_based_charging:
description: Enable or disable price-based charging
fields:
mqtt_prefix:
name: Prefix for MQTT topic
description: Prefix on the MQTT server that addresses the respective wallbox
default: 'openWB/openWB'
example: 'openWB/openWB'
required: true
selector:
text:
selected_status:
name: Price-based Charging
description: Desired price-based charging status
default: 'Off'
required: true
selector:
select:
options:
- 'On'
- 'Off'
charge_point_id:
name: Charge point ID
description: ID of the charge point which shall be enabled / disabled
default: 1
required: true
selector:
number:
min: 1
max: 8
12 changes: 6 additions & 6 deletions custom_components/openwbmqtt/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ async def async_setup_entry(
for chargePoint in range(1, nChargePoints + 1):
localSwitchesPerLP = copy.deepcopy(SWITCHES_PER_LP)
for description in localSwitchesPerLP:
description.mqttTopicCommand = (
f"{mqttRoot}/set/lp{str(chargePoint)}/{description.mqttTopicCommand}"
)
description.mqttTopicCurrentValue = (
f"{mqttRoot}/lp/{str(chargePoint)}/{description.mqttTopicCurrentValue}"
)
if description.mqttTopicChargeMode:
description.mqttTopicCommand = f"{mqttRoot}/config/set/{str(description.mqttTopicChargeMode)}/lp/{str(chargePoint)}/{description.mqttTopicCommand}"
description.mqttTopicCurrentValue = f"{mqttRoot}/config/get/{str(description.mqttTopicChargeMode)}/lp/{str(chargePoint)}/{description.mqttTopicCurrentValue}"
else: # for manual SoC module
description.mqttTopicCommand = f"{mqttRoot}/set/lp/{str(chargePoint)}/{description.mqttTopicCommand}"
description.mqttTopicCurrentValue = f"{mqttRoot}/lp/{str(chargePoint)}/{description.mqttTopicCurrentValue}"
switchList.append(
openwbSwitch(
unique_id=integrationUniqueID,
Expand Down

0 comments on commit cbab475

Please sign in to comment.