Skip to content

Commit 93ffe57

Browse files
authored
Merge pull request #17 from drc38/available
ocpp: set unavailable state on charger disconnect
2 parents 8c6a8bb + aa3972f commit 93ffe57

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

custom_components/ocpp/api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Dict
77

88
from homeassistant.config_entries import ConfigEntry
9-
from homeassistant.const import TIME_MINUTES
9+
from homeassistant.const import STATE_OK, STATE_UNAVAILABLE, TIME_MINUTES
1010
from homeassistant.core import HomeAssistant
1111
from homeassistant.helpers import device_registry, entity_component, entity_registry
1212
import voluptuous as vol
@@ -163,6 +163,7 @@ async def on_connect(self, websocket, path: str):
163163
except Exception as e:
164164
_LOGGER.info(f"Exception occurred:\n{e}")
165165
finally:
166+
self.charge_points[cp_id].status = STATE_UNAVAILABLE
166167
_LOGGER.info(f"Charger {cp_id} disconnected from {self.host}:{self.port}.")
167168

168169
def get_metric(self, cp_id: str, measurand: str):
@@ -183,6 +184,12 @@ def get_extra_attr(self, cp_id: str, measurand: str):
183184
return self.charge_points[cp_id].get_extra_attr(measurand)
184185
return None
185186

187+
def get_available(self, cp_id: str):
188+
"""Return whether the charger is available."""
189+
if cp_id in self.charge_points:
190+
return self.charge_points[cp_id].status == STATE_OK
191+
return False
192+
186193
async def set_charger_state(
187194
self, cp_id: str, service_name: str, state: bool = True
188195
):
@@ -349,6 +356,7 @@ async def handle_get_configuration(call):
349356
handle_update_firmware,
350357
UFW_SERVICE_DATA_SCHEMA,
351358
)
359+
self.status = STATE_OK
352360
except (NotImplementedError) as e:
353361
_LOGGER.error("Configuration of the charger failed: %s", e)
354362

custom_components/ocpp/sensor.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ def state(self):
6767
"""Return the state of the sensor."""
6868
return self.central_system.get_metric(self.cp_id, self.metric)
6969

70+
@property
71+
def available(self) -> bool:
72+
"""Return if switch is available."""
73+
return self.central_system.get_available(self.cp_id)
74+
7075
@property
7176
def unit_of_measurement(self):
7277
"""Return the unit the value is expressed in."""

custom_components/ocpp/switch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def unique_id(self):
4545
@property
4646
def available(self) -> bool:
4747
"""Return if switch is available."""
48-
return True # type: ignore [no-any-return]
48+
return self.central_system.get_available(self.cp_id) # type: ignore [no-any-return]
4949

5050
@property
5151
def is_on(self) -> bool:

0 commit comments

Comments
 (0)