Skip to content

Commit df656d1

Browse files
author
lbbrhzn
authored
add switch and device information (#35)
Co-authored-by: lbbrhzn <@lbbrhzn>
1 parent 1d17392 commit df656d1

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

custom_components/ocpp/central_system.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import websockets
55

66
from .charge_point import ChargePoint
7-
from .const import DEFAULT_HOST, DEFAULT_PORT, DEFAULT_SUBPROTOCOL
7+
from .const import CONF_NAME, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_SUBPROTOCOL, DOMAIN
88

99
_LOGGER = logging.getLogger(__name__)
1010

@@ -69,3 +69,14 @@ def get_unit(self, measurand: str):
6969
if self._connected_charger is not None:
7070
return self._connected_charger.get_unit(measurand)
7171
return None
72+
73+
def device_info(self):
74+
"""Return device information."""
75+
return {
76+
"identifiers": {(DOMAIN, self.id)},
77+
"name": self.config[CONF_NAME],
78+
"model": self.get_metric("Model"),
79+
"manufacturer": self.get_metric("Vendor"),
80+
"sw_version": self.get_metric("FW.Version"),
81+
"suggested_area": "Garage",
82+
}

custom_components/ocpp/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
BINARY_SENSOR = "binary_sensor"
2525
SENSOR = "sensor"
2626
SWITCH = "switch"
27-
PLATFORMS = [SENSOR]
27+
PLATFORMS = [SENSOR, SWITCH]
2828

2929
# Ocpp SupportedFeatureProfiles
3030
FEATURE_PROFILE_CORE = "Core"

custom_components/ocpp/sensor.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ async def async_setup_entry(hass, entry, async_add_devices):
1010
"""Configure the sensor platform."""
1111
central_sys = hass.data[DOMAIN][entry.entry_id]
1212

13-
metrics = []
13+
entities = []
14+
1415
for measurand in entry.data[CONF_MONITORED_VARIABLES].split(","):
15-
metrics.append(
16+
entities.append(
1617
ChargePointMetric(measurand, central_sys, "M", entry.data[CONF_NAME])
1718
)
1819
for condition in CONDITIONS:
19-
metrics.append(
20+
entities.append(
2021
ChargePointMetric(condition, central_sys, "S", entry.data[CONF_NAME])
2122
)
2223
for gen in GENERAL:
23-
metrics.append(ChargePointMetric(gen, central_sys, "G", entry.data[CONF_NAME]))
24+
entities.append(ChargePointMetric(gen, central_sys, "G", entry.data[CONF_NAME]))
2425

25-
async_add_devices(metrics)
26+
async_add_devices(entities)
2627

2728

2829
class ChargePointMetric(Entity):
@@ -32,6 +33,7 @@ def __init__(self, metric, central_sys, genre, prefix):
3233
"""Instantiate instance of a ChargePointMetrics."""
3334
self.metric = metric
3435
self.central_sys = central_sys
36+
self._id = ".".join([DOMAIN, "sensor", self.central_sys.id, self.metric])
3537
self._genre = genre
3638
self.prefix = prefix
3739
self._state = None
@@ -40,13 +42,13 @@ def __init__(self, metric, central_sys, genre, prefix):
4042
@property
4143
def name(self):
4244
"""Return the name of the sensor."""
43-
return self.prefix + "." + self.metric
45+
return DOMAIN + "." + self.prefix + "." + self.metric
4446

4547
@property
4648
def unique_id(self):
4749
"""Return the unique id of this sensor."""
4850
# This may need to be improved, perhaps use the vendor, model and serial number?
49-
return ".".join(["sensor", self.central_sys.id, self.metric])
51+
return self._id
5052

5153
@property
5254
def state(self):
@@ -68,6 +70,19 @@ def icon(self):
6870
"""Return the icon to use in the frontend, if any."""
6971
return ICON
7072

73+
@property
74+
def device_info(self):
75+
"""Return device information."""
76+
return self.central_sys.device_info()
77+
78+
@property
79+
def extra_state_attributes(self):
80+
"""Return the state attributes."""
81+
return {
82+
"unique_id": self.unique_id,
83+
"integration": DOMAIN,
84+
}
85+
7186
def update(self):
7287
"""Get the latest data and update the states."""
7388
pass

0 commit comments

Comments
 (0)