Skip to content

Commit

Permalink
Revert "register services to cpid"
Browse files Browse the repository at this point in the history
This reverts commit 6c32421.
  • Loading branch information
drc38 committed Jan 16, 2025
1 parent 6c32421 commit 5fb4b05
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 50 deletions.
6 changes: 2 additions & 4 deletions custom_components/ocpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,11 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if entry.entry_id in hass.data[DOMAIN]:
# Close server
central_sys = hass.data[DOMAIN][entry.entry_id]
cpids = central_sys.cpids.keys()
central_sys._server.close()
await central_sys._server.wait_closed()
# Unload services
for cpid in cpids:
for service in hass.services.async_services_for_domain(cpid):
hass.services.async_remove(cpid, service)
for service in hass.services.async_services_for_domain(DOMAIN):
hass.services.async_remove(DOMAIN, service)
# Unload platforms
unloaded = await hass.config_entries.async_unload_platforms(
entry, PLATFORMS
Expand Down
12 changes: 5 additions & 7 deletions custom_components/ocpp/chargepoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,32 +339,30 @@ async def handle_set_charge_rate(call):
# Register custom services with home assistant
self.register_version_specific_services()
self.hass.services.async_register(
self.settings.cpid,
DOMAIN,
csvcs.service_data_transfer.value,
handle_data_transfer,
TRANS_SERVICE_DATA_SCHEMA,
)
if prof.SMART in self._attr_supported_features:
self.hass.services.async_register(
self.settings.cpid,
csvcs.service_clear_profile.value,
handle_clear_profile,
DOMAIN, csvcs.service_clear_profile.value, handle_clear_profile
)
self.hass.services.async_register(
self.settings.cpid,
DOMAIN,
csvcs.service_set_charge_rate.value,
handle_set_charge_rate,
CHRGR_SERVICE_DATA_SCHEMA,
)
if prof.FW in self._attr_supported_features:
self.hass.services.async_register(
self.settings.cpid,
DOMAIN,
csvcs.service_update_firmware.value,
handle_update_firmware,
UFW_SERVICE_DATA_SCHEMA,
)
self.hass.services.async_register(
self.settings.cpid,
DOMAIN,
csvcs.service_get_diagnostics.value,
handle_get_diagnostics,
GDIAG_SERVICE_DATA_SCHEMA,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/ocpp/ocppv16.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ async def handle_get_configuration(call):
await self.get_configuration(key)

self.hass.services.async_register(
self.settings.cpid,
DOMAIN,
csvcs.service_configure.value,
handle_configure,
CONF_SERVICE_DATA_SCHEMA,
)
self.hass.services.async_register(
self.settings.cpid,
DOMAIN,
csvcs.service_get_configuration.value,
handle_get_configuration,
GCONF_SERVICE_DATA_SCHEMA,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/ocpp/ocppv201.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ async def handle_get_configuration(call) -> ServiceResponse:
return {"value": value}

self.hass.services.async_register(
self.settings.cpid,
DOMAIN,
csvcs.service_configure_v201.value,
handle_configure,
CONF_SERVICE_DATA_SCHEMA,
supports_response=SupportsResponse.OPTIONAL,
)
self.hass.services.async_register(
self.settings.cpid,
DOMAIN,
csvcs.service_get_configuration_v201.value,
handle_get_configuration,
GCONF_SERVICE_DATA_SCHEMA,
Expand Down
6 changes: 3 additions & 3 deletions tests/charge_point_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ async def remove_configuration(hass: HomeAssistant, config_entry: MockConfigEntr
remove_configuration.__test__ = False


async def wait_ready(hass: HomeAssistant, cpid: str):
async def wait_ready(hass: HomeAssistant):
"""Wait until charge point is connected and initialised."""
hass.services.async_remove(cpid, csvcs.service_data_transfer)
while not hass.services.has_service(cpid, csvcs.service_data_transfer):
hass.services.async_remove(OCPP_DOMAIN, csvcs.service_data_transfer)
while not hass.services.has_service(OCPP_DOMAIN, csvcs.service_data_transfer):
await asyncio.sleep(0.1)


Expand Down
6 changes: 3 additions & 3 deletions tests/test_charge_point_v16.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ async def test_services(hass, cpid, serv_list, socket_enabled):
data = {"limit_amps": 30}

await hass.services.async_call(
cpid,
OCPP_DOMAIN,
service.value,
service_data=data,
blocking=True,
)
# test additional set charge rate options
await hass.services.async_call(
cpid,
OCPP_DOMAIN,
csvcs.service_set_charge_rate,
service_data={"limit_watts": 3000},
blocking=True,
Expand All @@ -123,7 +123,7 @@ async def test_services(hass, cpid, serv_list, socket_enabled):
}
data = {"custom_profile": str(prof)}
await hass.services.async_call(
cpid,
OCPP_DOMAIN,
csvcs.service_set_charge_rate,
service_data=data,
blocking=True,
Expand Down
54 changes: 25 additions & 29 deletions tests/test_charge_point_v201.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,13 +799,13 @@ async def _test_transaction(hass: HomeAssistant, cs: CentralSystem, cp: ChargePo


async def _set_variable(
hass: HomeAssistant, cs: CentralSystem, cpid: str, key: str, value: str
hass: HomeAssistant, cs: CentralSystem, cp: ChargePoint, key: str, value: str
) -> tuple[ServiceResponse, HomeAssistantError]:
response: ServiceResponse | None = None
error: HomeAssistantError | None = None
try:
response = await hass.services.async_call(
cpid,
OCPP_DOMAIN,
csvcs.service_configure_v201,
service_data={"ocpp_key": key, "value": value},
blocking=True,
Expand All @@ -817,13 +817,13 @@ async def _set_variable(


async def _get_variable(
hass: HomeAssistant, cs: CentralSystem, cpid: str, key: str
hass: HomeAssistant, cs: CentralSystem, cp: ChargePoint, key: str
) -> tuple[ServiceResponse, HomeAssistantError]:
response: ServiceResponse | None = None
error: HomeAssistantError | None = None
try:
response = await hass.services.async_call(
cpid,
OCPP_DOMAIN,
csvcs.service_get_configuration_v201,
service_data={"ocpp_key": key},
blocking=True,
Expand All @@ -838,72 +838,71 @@ async def _test_services(hass: HomeAssistant, cs: CentralSystem, cp: ChargePoint
service_response: ServiceResponse
error: HomeAssistantError

cpid: str = list(cs.cpids.keys())[0]
service_response, error = await _set_variable(
hass, cs, cpid, "SampledDataCtrlr/TxUpdatedInterval", "17"
hass, cs, cp, "SampledDataCtrlr/TxUpdatedInterval", "17"
)
assert service_response == {"reboot_required": False}
assert cp.tx_updated_interval == 17

service_response, error = await _set_variable(
hass, cs, cpid, "SampledDataCtrlr/RebootRequired", "17"
hass, cs, cp, "SampledDataCtrlr/RebootRequired", "17"
)
assert service_response == {"reboot_required": True}

service_response, error = await _set_variable(
hass, cs, cpid, "TestComponent(CompInstance)/TestVariable(VarInstance)", "17"
hass, cs, cp, "TestComponent(CompInstance)/TestVariable(VarInstance)", "17"
)
assert service_response == {"reboot_required": False}
assert cp.component_instance_used == "CompInstance"
assert cp.variable_instance_used == "VarInstance"

service_response, error = await _set_variable(
hass, cs, cpid, "SampledDataCtrlr/BadVariable", "17"
hass, cs, cp, "SampledDataCtrlr/BadVariable", "17"
)
assert error is not None
assert str(error).startswith("Failed to set variable")

service_response, error = await _set_variable(
hass, cs, cpid, "SampledDataCtrlr/VeryBadVariable", "17"
hass, cs, cp, "SampledDataCtrlr/VeryBadVariable", "17"
)
assert error is not None
assert str(error).startswith("OCPP call failed: InternalError")

service_response, error = await _set_variable(
hass, cs, cpid, "does not compute", "17"
hass, cs, cp, "does not compute", "17"
)
assert error is not None
assert str(error) == "Invalid OCPP key"

service_response, error = await _get_variable(
hass, cs, cpid, "SampledDataCtrlr/TxUpdatedInterval"
hass, cs, cp, "SampledDataCtrlr/TxUpdatedInterval"
)
assert service_response == {"value": "17"}

service_response, error = await _get_variable(
hass, cs, cpid, "TestComponent(CompInstance)/TestInstance(VarInstance)"
hass, cs, cp, "TestComponent(CompInstance)/TestInstance(VarInstance)"
)
assert service_response == {"value": "CompInstance,VarInstance"}

service_response, error = await _get_variable(
hass, cs, cpid, "SampledDataCtrlr/BadVariale"
hass, cs, cp, "SampledDataCtrlr/BadVariale"
)
assert error is not None
assert str(error).startswith("Failed to get variable")

service_response, error = await _get_variable(
hass, cs, cpid, "SampledDataCtrlr/VeryBadVariable"
hass, cs, cp, "SampledDataCtrlr/VeryBadVariable"
)
assert error is not None
assert str(error).startswith("OCPP call failed: InternalError")


async def _set_charge_rate_service(
hass: HomeAssistant, cpid: str, data: dict
hass: HomeAssistant, data: dict
) -> HomeAssistantError:
try:
await hass.services.async_call(
cpid,
OCPP_DOMAIN,
csvcs.service_set_charge_rate,
service_data=data,
blocking=True,
Expand All @@ -916,11 +915,11 @@ async def _set_charge_rate_service(
async def _test_charge_profiles(
hass: HomeAssistant, cs: CentralSystem, cp: ChargePoint
):
cpid: str = list(cs.cpids.keys())[0]
error: HomeAssistantError = await _set_charge_rate_service(
hass, cpid, {"limit_watts": 3000}
hass, {"limit_watts": 3000}
)

cpid: str = list(cs.cpids.keys())[0]
assert error is None
assert len(cp.charge_profiles_set) == 1
assert cp.charge_profiles_set[-1].evse_id == 0
Expand All @@ -938,7 +937,7 @@ async def _test_charge_profiles(
],
}

error = await _set_charge_rate_service(hass, cpid, {"limit_amps": 16})
error = await _set_charge_rate_service(hass, {"limit_amps": 16})
assert error is None
assert len(cp.charge_profiles_set) == 2
assert cp.charge_profiles_set[-1].evse_id == 0
Expand All @@ -958,7 +957,6 @@ async def _test_charge_profiles(

error = await _set_charge_rate_service(
hass,
cpid,
{
"custom_profile": """{
'id': 2,
Expand Down Expand Up @@ -1007,7 +1005,7 @@ async def _test_charge_profiles(
],
}

error = await _set_charge_rate_service(hass, cpid, {"limit_amps": 5})
error = await _set_charge_rate_service(hass, {"limit_amps": 5})
assert error is not None
assert str(error).startswith("Failed to set variable: Rejected")

Expand All @@ -1021,7 +1019,6 @@ async def _test_charge_profiles(


async def _run_test(hass: HomeAssistant, cs: CentralSystem, cp: ChargePoint):
cpid: str = list(cs.cpids.keys())[0]
boot_res: call_result.BootNotification = await cp.call(
call.BootNotification(
{
Expand All @@ -1045,11 +1042,12 @@ async def _run_test(hass: HomeAssistant, cs: CentralSystem, cp: ChargePoint):
heartbeat_resp: call_result.Heartbeat = await cp.call(call.Heartbeat())
datetime.fromisoformat(heartbeat_resp.current_time)

await wait_ready(hass, cpid)
await wait_ready(hass)

# Junk report to be ignored
await cp.call(call.NotifyReport(2, datetime.now(tz=UTC).isoformat(), 0))

cpid: str = list(cs.cpids.keys())[0]
assert cs.get_metric(cpid, cdet.serial.value) == "SERIAL"
assert cs.get_metric(cpid, cdet.model.value) == "MODEL"
assert cs.get_metric(cpid, cdet.vendor.value) == "VENDOR"
Expand Down Expand Up @@ -1139,7 +1137,6 @@ async def _extra_features_test(
cs: CentralSystem,
cp: ChargePointAllFeatures,
):
cpid = list(cs.cpids.keys())[0]
await cp.call(
call.BootNotification(
{
Expand All @@ -1151,7 +1148,7 @@ async def _extra_features_test(
BootReasonEnumType.power_up.value,
)
)
await wait_ready(hass, cpid)
await wait_ready(hass)

assert (
cs.get_metric(
Expand Down Expand Up @@ -1193,7 +1190,6 @@ async def _unsupported_base_report_test(
cs: CentralSystem,
cp: ChargePoint,
):
cpid = list(cs.cpids.keys())[0]
await cp.call(
call.BootNotification(
{
Expand All @@ -1205,10 +1201,10 @@ async def _unsupported_base_report_test(
BootReasonEnumType.power_up.value,
)
)
await wait_ready(hass, cpid)
await wait_ready(hass)
assert (
cs.get_metric(
cpid,
list(cs.cpids.keys())[0],
cdet.features.value,
)
== Profiles.CORE | Profiles.REM | Profiles.FW
Expand Down

0 comments on commit 5fb4b05

Please sign in to comment.