Skip to content

Commit

Permalink
Also consider chargepoint status in ChargeampsSensor
Browse files Browse the repository at this point in the history
Show the connector as unavailable if the chargepoint is not connected to
the Chargeamps API.
  • Loading branch information
viiru- committed Jan 31, 2024
1 parent 443b50d commit 952f2a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions custom_components/chargeamps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ChargePointConnector,
ChargePointConnectorSettings,
ChargePointConnectorStatus,
ChargePointStatus,
StartAuth,
)
from chargeamps.external import ChargeAmpsExternalClient
Expand Down Expand Up @@ -184,6 +185,9 @@ def get_chargepoint_total_energy(self, charge_point_id) -> float:
def get_chargepoint_info(self, charge_point_id) -> ChargePoint:
return self.hass.data[DOMAIN_DATA]["chargepoint_info"].get(charge_point_id)

def get_chargepoint_status(self, charge_point_id) -> ChargePointStatus:
return self.hass.data[DOMAIN_DATA]["chargepoint_status"].get(charge_point_id)

def get_chargepoint_settings(self, charge_point_id):
return self.hass.data[DOMAIN_DATA]["chargepoint_settings"].get(charge_point_id)

Expand Down
3 changes: 3 additions & 0 deletions custom_components/chargeamps/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@

# Overall scan interval
SCAN_INTERVAL = timedelta(seconds=10)

# Chargepoint online status
CHARGEPOINT_ONLINE = "Online"
10 changes: 7 additions & 3 deletions custom_components/chargeamps/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
SensorEntity,
SensorStateClass,
)
from homeassistant.const import UnitOfEnergy, UnitOfPower
from homeassistant.const import UnitOfEnergy, UnitOfPower, STATE_UNAVAILABLE

from . import ChargeampsEntity
from .const import DOMAIN_DATA, SCAN_INTERVAL # noqa
from .const import DOMAIN_DATA, SCAN_INTERVAL, CHARGEPOINT_ONLINE # noqa

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,12 +84,16 @@ async def async_update(self):
self.charge_point_id,
self.connector_id,
)
cp_status = self.handler.get_chargepoint_status(self.charge_point_id)
status = self.handler.get_connector_status(
self.charge_point_id, self.connector_id
)
if status is None:
return
self._state = status.status
if cp_status.status != CHARGEPOINT_ONLINE:
self._state = STATE_UNAVAILABLE
else:
self._state = status.status
self._attributes["total_consumption_kwh"] = round(
status.total_consumption_kwh, 3
)
Expand Down

0 comments on commit 952f2a8

Please sign in to comment.