Skip to content

Commit 7ab2616

Browse files
author
Jan Thunqvist
committed
Fix max_current per connector bug. Remove ChargerSession sensors (used for debugging). Fix Charge Control naming when only one connector. More CodeRabbit suggestions.
1 parent 2e7d429 commit 7ab2616

File tree

7 files changed

+35
-29
lines changed

7 files changed

+35
-29
lines changed

custom_components/ocpp/button.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class OcppButtonDescription(ButtonEntityDescription):
3838
OcppButtonDescription(
3939
key="unlock",
4040
name="Unlock",
41-
device_class=ButtonDeviceClass.UPDATE,
41+
device_class=None,
4242
entity_category=EntityCategory.CONFIG,
4343
press_action=HAChargerServices.service_unlock.name,
4444
per_connector=True,

custom_components/ocpp/number.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ def __init__(
157157
)
158158
self._attr_native_value = self.entity_description.initial_value
159159
self._attr_should_poll = False
160-
self._attr_available = True
161160

162161
async def async_added_to_hass(self) -> None:
163162
"""Handle entity which will be added."""
@@ -188,7 +187,7 @@ async def async_set_native_value(self, value):
188187
resp = await self.central_system.set_max_charge_rate_amps(
189188
self.cpid,
190189
num_value,
191-
connector_id=0,
190+
connector_id=self._op_connector_id,
192191
)
193192
if resp is True:
194193
self._attr_native_value = num_value

custom_components/ocpp/ocppv201.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ def _apply_status_notification(
165165
):
166166
"""Update per connector and evse aggregated."""
167167
if evse_id > len(self._connector_status):
168-
self._connector_status += [[]] * (evse_id - len(self._connector_status))
168+
needed = evse_id - len(self._connector_status)
169+
self._connector_status.extend([[] for _ in range(needed)])
169170
if connector_id > len(self._connector_status[evse_id - 1]):
170171
self._connector_status[evse_id - 1] += [None] * (
171172
connector_id - len(self._connector_status[evse_id - 1])
@@ -346,9 +347,18 @@ async def set_charge_rate(
346347
conn_id: int = 0,
347348
profile: dict | None = None,
348349
):
349-
"""Set a charging profile with defined limit (OCPP 2.x)."""
350+
"""Set a charging profile with defined limit (OCPP 2.x).
351+
352+
- conn_id=0 (default) targets the Charging Station (evse_id=0).
353+
- conn_id>0 targets the specific EVSE corresponding to the global connector index.
354+
"""
355+
356+
evse_target = 0
357+
if conn_id and conn_id > 0:
358+
with contextlib.suppress(Exception):
359+
evse_target, _ = self._global_to_pair(int(conn_id))
350360
if profile is not None:
351-
req = call.SetChargingProfile(0, profile)
361+
req = call.SetChargingProfile(evse_target, profile)
352362
resp: call_result.SetChargingProfile = await self.call(req)
353363
if resp.status != ChargingProfileStatusEnumType.accepted:
354364
raise HomeAssistantError(
@@ -394,7 +404,9 @@ async def set_charge_rate(
394404
"charging_schedule": [schedule],
395405
}
396406

397-
req: call.SetChargingProfile = call.SetChargingProfile(0, charging_profile)
407+
req: call.SetChargingProfile = call.SetChargingProfile(
408+
evse_target, charging_profile
409+
)
398410
resp: call_result.SetChargingProfile = await self.call(req)
399411
if resp.status != ChargingProfileStatusEnumType.accepted:
400412
raise HomeAssistantError(
@@ -859,6 +871,7 @@ def on_transaction_event(
859871
if event_type == TransactionEventEnumType.ended.value:
860872
self._metrics[(global_idx, csess.transaction_id.value)].value = ""
861873
self._metrics[(global_idx, cstat.id_tag.value)].value = ""
874+
self._tx_start_time.pop(global_idx, None)
862875

863876
if not offline:
864877
self.hass.async_create_task(self.update(self.settings.cpid))

custom_components/ocpp/sensor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
ICON,
2929
Measurand,
3030
)
31-
from .enums import HAChargerDetails, HAChargerSession, HAChargerStatuses
31+
from .enums import HAChargerDetails, HAChargerStatuses
3232

3333

3434
@dataclass
@@ -50,10 +50,10 @@ async def async_setup_entry(hass, entry, async_add_devices):
5050

5151
configured = [
5252
m.strip()
53-
for m in cp_id_settings[CONF_MONITORED_VARIABLES].split(",")
53+
for m in str(cp_id_settings.get(CONF_MONITORED_VARIABLES, "")).split(",")
5454
if m and m.strip()
5555
]
56-
measurands = list(set(configured + list(HAChargerSession)))
56+
measurands = sorted(configured)
5757

5858
CHARGER_ONLY = [
5959
HAChargerStatuses.status.value,

custom_components/ocpp/switch.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,11 @@ def __init__(
121121
self._flatten_single = flatten_single
122122
self._state = self.entity_description.default_state
123123
parts = [SWITCH_DOMAIN, DOMAIN, cpid]
124-
if self.connector_id:
124+
if self.connector_id and not self._flatten_single:
125125
parts.append(f"conn{self.connector_id}")
126126
parts.append(description.key)
127127
self._attr_unique_id = ".".join(parts)
128128
self._attr_name = self.entity_description.name
129-
if self.entity_description.per_connector and self.connector_id:
130-
if self._flatten_single:
131-
self._attr_name = (
132-
f"Connector {self.connector_id} {self.entity_description.name}"
133-
)
134129
if self.connector_id and not self._flatten_single:
135130
self._attr_device_info = DeviceInfo(
136131
identifiers={(DOMAIN, f"{cpid}-conn{self.connector_id}")},
@@ -174,18 +169,14 @@ def is_on(self) -> bool:
174169

175170
async def async_turn_on(self, **kwargs):
176171
"""Turn the switch on."""
177-
target_conn = (
178-
self.connector_id if self.entity_description.per_connector else None
179-
)
172+
target_conn = self.connector_id if self.entity_description.per_connector else 0
180173
self._state = await self.central_system.set_charger_state(
181174
self.cpid, self.entity_description.on_action, True, connector_id=target_conn
182175
)
183176

184177
async def async_turn_off(self, **kwargs):
185178
"""Turn the switch off."""
186-
target_conn = (
187-
self.connector_id if self.entity_description.per_connector else None
188-
)
179+
target_conn = self.connector_id if self.entity_description.per_connector else 0
189180
if self.entity_description.off_action is None:
190181
resp = True
191182
elif self.entity_description.off_action == self.entity_description.on_action:

scripts/develop

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ set -e
33

44
cd "$(dirname "$0")/.."
55

6-
# Create config dir if not present
7-
if [[ ! -d "${PWD}/config" ]]; then
8-
mkdir -p "${PWD}/config"
9-
/home/vscode/.local/ha-venv/bin/python -m homeassistant --config "${PWD}/config" --script ensure_config
6+
# Create/prepare config
7+
mkdir -p "${PWD}/config"
8+
if [[ ! -f "${PWD}/config/configuration.yaml" ]]; then
9+
/home/vscode/.local/ha-venv/bin/python -m homeassistant --config "${PWD}/config" --script ensure_config
1010
fi
1111

1212
# Ensure custom components dir exists
@@ -19,7 +19,11 @@ if [[ ! -d "$src" ]]; then
1919
echo "Missing integration sources at: $src" >&2
2020
exit 1
2121
fi
22-
ln -sfn "$src" "$dst"
22+
23+
# Remove existing dir/symlink/file at destination
24+
if [[ -e "$dst" || -L "$dst" ]]; then
25+
rm -rf -- "$dst"
26+
fi
2327

2428
# Install debugpy if missing
2529
if ! /home/vscode/.local/ha-venv/bin/python -c 'import debugpy' >/dev/null 2>&1; then

tests/charge_point_test.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828

2929
async def set_switch(hass: HomeAssistant, cpid: str, key: str, on: bool):
3030
"""Toggle a switch."""
31-
prefix = "_" if key == "availability" else "_connector_1_"
3231
await hass.services.async_call(
3332
SWITCH_DOMAIN,
3433
SERVICE_TURN_ON if on else SERVICE_TURN_OFF,
35-
service_data={ATTR_ENTITY_ID: f"{SWITCH_DOMAIN}.{cpid}{prefix}{key}"},
34+
service_data={ATTR_ENTITY_ID: f"{SWITCH_DOMAIN}.{cpid}_{key}"},
3635
blocking=True,
3736
)
3837

0 commit comments

Comments
 (0)