Skip to content

Commit

Permalink
improve test coverage (lbbrhzn#346)
Browse files Browse the repository at this point in the history
Co-authored-by: lbbrhzn <@lbbrhzn>
  • Loading branch information
lbbrhzn authored Feb 2, 2022
1 parent 9debb5f commit 2af939d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
29 changes: 11 additions & 18 deletions custom_components/ocpp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,22 +180,16 @@ async def on_connect(
_LOGGER.info(f"Charger websocket path={path}")
cp_id = path.strip("/")
cp_id = cp_id[cp_id.rfind("/") + 1 :]
try:
if self.cpid not in self.charge_points:
_LOGGER.info(f"Charger {cp_id} connected to {self.host}:{self.port}.")
charge_point = ChargePoint(
cp_id, websocket, self.hass, self.entry, self
)
self.charge_points[self.cpid] = charge_point
await charge_point.start()
else:
_LOGGER.info(f"Charger {cp_id} reconnected to {self.host}:{self.port}.")
charge_point: ChargePoint = self.charge_points[self.cpid]
await charge_point.reconnect(websocket)
except Exception as e:
_LOGGER.error(f"Exception occurred:\n{e}", exc_info=True)
finally:
_LOGGER.info(f"Charger {cp_id} disconnected from {self.host}:{self.port}.")
if self.cpid not in self.charge_points:
_LOGGER.info(f"Charger {cp_id} connected to {self.host}:{self.port}.")
charge_point = ChargePoint(cp_id, websocket, self.hass, self.entry, self)
self.charge_points[self.cpid] = charge_point
await charge_point.start()
else:
_LOGGER.info(f"Charger {cp_id} reconnected to {self.host}:{self.port}.")
charge_point: ChargePoint = self.charge_points[self.cpid]
await charge_point.reconnect(websocket)
_LOGGER.info(f"Charger {cp_id} disconnected from {self.host}:{self.port}.")

def get_metric(self, cp_id: str, measurand: str):
"""Return last known value for given measurand."""
Expand Down Expand Up @@ -243,6 +237,7 @@ async def set_charger_state(
self, cp_id: str, service_name: str, state: bool = True
):
"""Carry out requested service/state change on connected charger."""
resp = False
if cp_id in self.charge_points:
if service_name == csvcs.service_availability.name:
resp = await self.charge_points[cp_id].set_availability(state)
Expand All @@ -254,8 +249,6 @@ async def set_charger_state(
resp = await self.charge_points[cp_id].reset()
if service_name == csvcs.service_unlock.name:
resp = await self.charge_points[cp_id].unlock()
else:
resp = False
return resp

async def update(self, cp_id: str):
Expand Down
50 changes: 47 additions & 3 deletions tests/test_charge_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,38 @@ async def test_services(hass, socket_enabled):

cs = hass.data[OCPP_DOMAIN][config_entry.entry_id]

# no subprotocol
async with websockets.connect(
"ws://127.0.0.1:9000/CP_1",
subprotocols=["unsupported_subprotocol"],
) as ws:
# use a different id for debugging
cp = ChargePoint("CP_1_unsupported", ws)
cp = ChargePoint("CP_1_no_subprotocol", ws)
try:
await asyncio.wait_for(
asyncio.gather(
cp.start(),
cp.send_boot_notification(),
cp.send_authorize(),
cp.send_heartbeat(),
cp.send_status_notification(),
cp.send_firmware_status(),
cp.send_data_transfer(),
cp.send_start_transaction(),
cp.send_stop_transaction(),
cp.send_meter_data(),
),
timeout=3,
)
except websockets.exceptions.ConnectionClosedOK:
pass

# unsupported subprotocol
async with websockets.connect(
"ws://127.0.0.1:9000/CP_1",
subprotocols=["ocpp0.0"],
) as ws:
# use a different id for debugging
cp = ChargePoint("CP_1_unsupported_subprotocol", ws)
try:
await asyncio.wait_for(
asyncio.gather(
Expand Down Expand Up @@ -187,6 +213,24 @@ async def test_services(hass, socket_enabled):
assert int(cs.get_metric("test_cpid", "Current.Import")) == int(20)
assert int(cs.get_metric("test_cpid", "Voltage")) == int(228)
assert cs.get_unit("test_cpid", "Energy.Active.Import.Register") == "kWh"
assert cs.get_metric("unknown_cpid", "Energy.Active.Import.Register") is None
assert cs.get_unit("unknown_cpid", "Energy.Active.Import.Register") is None
assert cs.get_extra_attr("unknown_cpid", "Energy.Active.Import.Register") is None
assert int(cs.get_supported_features("unknown_cpid")) == int(0)
assert (
await asyncio.wait_for(
cs.set_max_charge_rate_amps("unknown_cpid", 0), timeout=1
)
is False
)
assert (
await asyncio.wait_for(
cs.set_charger_state("unknown_cpid", csvcs.service_clear_profile, False),
timeout=1,
)
is False
)

await asyncio.sleep(1)
# test ocpp messages sent from cms to charger, through HA switches/services
# should reconnect as already started above
Expand Down Expand Up @@ -234,7 +278,7 @@ def on_get_configuration(self, key, **kwargs):
{
"key": key[0],
"readonly": False,
"value": "Core,FirmwareManagement,RemoteTrigger,SmartCharging",
"value": "Core,FirmwareManagement,LocalAuthListManagement,Reservation,SmartCharging,RemoteTrigger",
}
]
)
Expand Down

0 comments on commit 2af939d

Please sign in to comment.