Skip to content

Commit c552c97

Browse files
authored
Merge pull request #103 from drc38/tests
tests: improve switch testing
2 parents b9ff350 + db49144 commit c552c97

File tree

5 files changed

+70
-106
lines changed

5 files changed

+70
-106
lines changed

custom_components/ocpp/const.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Define constants for OCPP integration."""
22
import homeassistant.const as ha
33

4-
from ocpp.v16.enums import Measurand, UnitOfMeasure
4+
from ocpp.v16.enums import ChargePointStatus, Measurand, UnitOfMeasure
5+
6+
from .enums import HAChargerServices, HAChargerStatuses
57

68
DOMAIN = "ocpp"
79
CONF_METER_INTERVAL = "meter_interval"
@@ -63,26 +65,32 @@
6365
HA_ENERGY_UNIT = UnitOfMeasure.kwh.value
6466
HA_POWER_UNIT = UnitOfMeasure.kw.value
6567

66-
# Additional conditions/states to monitor
67-
CONDITIONS = [
68-
"Status",
69-
"Heartbeat",
70-
"Error.Code",
71-
"Stop.Reason",
72-
"FW.Status",
73-
"Session.Time", # in min
74-
"Session.Energy", # in kWh
75-
"Meter.Start", # in kWh
76-
]
77-
78-
# Additional general information to report
79-
GENERAL = [
80-
"ID",
81-
"Model",
82-
"Vendor",
83-
"Serial",
84-
"FW.Version",
85-
"Features",
86-
"Connectors",
87-
"Transaction.Id",
88-
]
68+
# Switch configuration definitions
69+
# At a minimum define switch name and on service call, pulse used to call a service once such as reset
70+
# metric and condition combination can be used to drive switch state, use default to set initial state to True
71+
SWITCH_CHARGE = {
72+
"name": "Charge_Control",
73+
"on": HAChargerServices.service_charge_start.name,
74+
"off": HAChargerServices.service_charge_stop.name,
75+
"metric": HAChargerStatuses.status.value,
76+
"condition": ChargePointStatus.charging.value,
77+
}
78+
SWITCH_AVAILABILITY = {
79+
"name": "Availability",
80+
"on": HAChargerServices.service_availability.name,
81+
"off": HAChargerServices.service_availability.name,
82+
"default": True,
83+
"metric": HAChargerStatuses.status.value,
84+
"condition": ChargePointStatus.available.value,
85+
}
86+
SWITCH_RESET = {
87+
"name": "Reset",
88+
"on": HAChargerServices.service_reset.name,
89+
"pulse": True,
90+
}
91+
SWITCH_UNLOCK = {
92+
"name": "Unlock",
93+
"on": HAChargerServices.service_unlock.name,
94+
"pulse": True,
95+
}
96+
SWITCHES = [SWITCH_CHARGE, SWITCH_RESET, SWITCH_UNLOCK, SWITCH_AVAILABILITY]

custom_components/ocpp/switch.py

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,8 @@
33

44
from homeassistant.components.switch import SwitchEntity
55

6-
from ocpp.v16.enums import ChargePointStatus
7-
86
from .api import CentralSystem
9-
from .const import CONF_CPID, DOMAIN, ICON
10-
from .enums import HAChargerServices, HAChargerStatuses
11-
12-
# At a minimum define switch name and on service call, pulse used to call a service once such as reset
13-
# metric and condition combination can be used to drive switch state, use default to set initial state to True
14-
SWITCH_CHARGE = {
15-
"name": "Charge_Control",
16-
"on": HAChargerServices.service_charge_start.name,
17-
"off": HAChargerServices.service_charge_stop.name,
18-
"metric": HAChargerStatuses.status.value,
19-
"condition": ChargePointStatus.charging.value,
20-
}
21-
SWITCH_AVAILABILITY = {
22-
"name": "Availability",
23-
"on": HAChargerServices.service_availability.name,
24-
"off": HAChargerServices.service_availability.name,
25-
"default": True,
26-
"metric": HAChargerStatuses.status.value,
27-
"condition": ChargePointStatus.available.value,
28-
}
29-
SWITCH_RESET = {
30-
"name": "Reset",
31-
"on": HAChargerServices.service_reset.name,
32-
"pulse": True,
33-
}
34-
SWITCH_UNLOCK = {
35-
"name": "Unlock",
36-
"on": HAChargerServices.service_unlock.name,
37-
"pulse": True,
38-
}
7+
from .const import CONF_CPID, DOMAIN, ICON, SWITCHES
398

409

4110
async def async_setup_entry(hass, entry, async_add_devices):
@@ -45,7 +14,7 @@ async def async_setup_entry(hass, entry, async_add_devices):
4514

4615
entities = []
4716

48-
for ent in [SWITCH_CHARGE, SWITCH_AVAILABILITY, SWITCH_RESET, SWITCH_UNLOCK]:
17+
for ent in SWITCHES:
4918
entities.append(ChargePointSwitch(central_system, cp_id, ent))
5019

5120
async_add_devices(entities, False)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ branch = False
4141

4242
[coverage:report]
4343
show_missing = true
44-
fail_under = 80
44+
fail_under = 85
4545

tests/test_charge_point.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import asyncio
33
from datetime import datetime, timezone # timedelta,
44

5+
from homeassistant.components.switch import SERVICE_TURN_OFF, SERVICE_TURN_ON
6+
from homeassistant.const import ATTR_ENTITY_ID
57
from pytest_homeassistant_custom_component.common import MockConfigEntry
68
import websockets
79

810
from custom_components.ocpp import async_setup_entry, async_unload_entry
9-
from custom_components.ocpp.const import DOMAIN
11+
from custom_components.ocpp.const import DOMAIN, SWITCH, SWITCHES
1012
from custom_components.ocpp.enums import ConfigurationKey
1113
from ocpp.routing import on
1214
from ocpp.v16 import ChargePoint as cpclass, call, call_result
@@ -33,6 +35,31 @@
3335

3436
async def test_cms_responses(hass):
3537
"""Test central system responses to a charger."""
38+
39+
async def test_switches(hass):
40+
"""Test switch operations."""
41+
42+
for switch in SWITCHES:
43+
result = await hass.services.async_call(
44+
SWITCH,
45+
SERVICE_TURN_ON,
46+
service_data={
47+
ATTR_ENTITY_ID: f"{SWITCH}.test_cpid_{switch['name'].lower()}"
48+
},
49+
blocking=True,
50+
)
51+
assert result
52+
53+
result = await hass.services.async_call(
54+
SWITCH,
55+
SERVICE_TURN_OFF,
56+
service_data={
57+
ATTR_ENTITY_ID: f"{SWITCH}.test_cpid_{switch['name'].lower()}"
58+
},
59+
blocking=True,
60+
)
61+
assert result
62+
3663
# Create a mock entry so we don't have to go through config flow
3764
config_entry = MockConfigEntry(
3865
domain=DOMAIN, data=MOCK_CONFIG_DATA, entry_id="test_cms"
@@ -69,8 +96,9 @@ async def test_cms_responses(hass):
6996
"http://www.charger.com/file.bin"
7097
),
7198
cs.charge_points["test_cpid"].unlock(),
99+
test_switches(hass),
72100
),
73-
timeout=7,
101+
timeout=5,
74102
)
75103
except asyncio.TimeoutError:
76104
pass
@@ -175,6 +203,11 @@ def on_remote_start_transaction(self, **kwargs):
175203
"""Handle remote start request."""
176204
return call_result.RemoteStartTransactionPayload(RemoteStartStopStatus.accepted)
177205

206+
@on(Action.RemoteStopTransaction)
207+
def on_remote_stop_transaction(self, **kwargs):
208+
"""Handle remote stop request."""
209+
return call_result.RemoteStopTransactionPayload(RemoteStartStopStatus.accepted)
210+
178211
@on(Action.SetChargingProfile)
179212
def on_set_charging_profile(self, **kwargs):
180213
"""Handle set charging profile request."""

tests/test_switch.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)