From 4458074c83673dc1ae110b616c41ffc194ede408 Mon Sep 17 00:00:00 2001 From: Jimmy Everling Date: Thu, 2 Sep 2021 20:42:34 +0200 Subject: [PATCH] Added support for both old and new api endpoints Modified sensors to try to get them to show up under energy --- custom_components/garo_wallbox/garo.py | 16 +++++++++++ custom_components/garo_wallbox/sensor.py | 35 ++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/custom_components/garo_wallbox/garo.py b/custom_components/garo_wallbox/garo.py index 0712045..2a01bc8 100644 --- a/custom_components/garo_wallbox/garo.py +++ b/custom_components/garo_wallbox/garo.py @@ -55,6 +55,7 @@ def __init__(self, host, name, session): self.name = name self._status = None self._session = session + self._pre_v1_3 = False async def init(self): await self.async_get_info() @@ -86,11 +87,23 @@ async def async_update(self): async def _do_update(self): response = await self._session.request(method='GET', url=self.__get_url('status', True)) + if response.status != 200 and not self._pre_v1_3: + self._pre_v1_3 = True + _LOGGER.info('Switching to pre v1.3.1 endpoint') + response = await self._session.request(method='GET', url=self.__get_url('status', True)) + + response_json = await response.json() self._status = GaroStatus(response_json) async def async_get_info(self): response = await self._session.request(method='GET', url=self.__get_url('config', True)) + _LOGGER.info(f'Response {response}') + if response.status != 200 and not self._pre_v1_3: + self._pre_v1_3 = True + _LOGGER.info('Switching to pre v1.3.1 endpoint') + response = await self._session.request(method='GET', url=self.__get_url('config', True)) + response_json = await response.json() self.info = GaroDeviceInfo(response_json) @@ -116,6 +129,8 @@ async def set_current_limit(self, limit): def __get_url(self, action, add_tick = False): + if self._pre_v1_3: + return 'http://{}:2222/rest/chargebox/{}{}'.format(self.host, action, '' if add_tick == False else '?_={}'.format(current_milli_time())) return 'http://{}:8080/servlet/rest/chargebox/{}{}'.format(self.host, action, '' if add_tick == False else '?_={}'.format(current_milli_time())) class GaroStatus: @@ -136,6 +151,7 @@ def __init__(self,response): self.current_charging_power = 0 self.acc_session_energy = response['accSessionEnergy'] self.latest_reading = response['latestReading'] + self.latest_reading_k = max(0,response['latestReading'] /1000) self.current_temperature = response['currentTemperature'] self.pilot_level = response['pilotLevel'] self.session_start_value = response['sessionStartValue'] diff --git a/custom_components/garo_wallbox/sensor.py b/custom_components/garo_wallbox/sensor.py index cdf590b..84c46e8 100644 --- a/custom_components/garo_wallbox/sensor.py +++ b/custom_components/garo_wallbox/sensor.py @@ -2,8 +2,19 @@ import voluptuous as vol -from homeassistant.const import CONF_ICON, CONF_NAME, TEMP_CELSIUS +from homeassistant.const import ( + CONF_ICON, + CONF_NAME, + TEMP_CELSIUS) from homeassistant.helpers.entity import Entity +from homeassistant.components.sensor import ( + DEVICE_CLASS_ENERGY, + DEVICE_CLASS_POWER, + PLATFORM_SCHEMA, + #STATE_CLASS_TOTAL_INCREASING, + STATE_CLASS_MEASUREMENT, + SensorEntity, +) from homeassistant.helpers import config_validation as cv, entity_platform, service from . import DOMAIN as GARO_DOMAIN @@ -31,6 +42,7 @@ async def async_setup_entry(hass, entry, async_add_entities): GaroSensor(device, "Pilot Level", 'pilot_level', 'A'), GaroSensor(device, "Session Energy", 'acc_session_energy', "Wh"), GaroSensor(device, "Total Energy", 'latest_reading', "Wh"), + GaroSensor(device, "Total Energy (kWh)", 'latest_reading_k', "kWh"), GaroSensor(device, "Temperature", 'current_temperature', TEMP_CELSIUS), ]) @@ -105,13 +117,18 @@ def device_state_attributes(self): return attrs -class GaroSensor(Entity): +class GaroSensor(SensorEntity): def __init__(self, device: GaroDevice, name, sensor, unit = None): """Initialize the sensor.""" self._device = device self._name = f"{device.name} {name}" self._sensor = sensor self._unit = unit + if self._sensor == "latest_reading" or self._sensor == "latest_reading_k": + _LOGGER.info(f'Initiating State sensors {self._name}') + self._attr_state_class = STATE_CLASS_MEASUREMENT #STATE_CLASS_TOTAL_INCREASING + self._attr_device_class = DEVICE_CLASS_ENERGY + @property def unique_id(self): @@ -139,7 +156,9 @@ def icon(self): icon = "mdi:flash" elif self._sensor == "acc_session_energy": icon = "mdi:flash" - elif self._sensor == "latest_reading": + elif self._sensor == "latest_reading": + icon = "mdi:flash" + elif self._sensor == "latest_reading_k": icon = "mdi:flash" elif self._sensor == "status": switcher = { @@ -171,6 +190,16 @@ def icon(self): icon = "mdi:google-circles-communities" return icon + @property + def native_value(self): + """Return the state of the sensor.""" + return round(self.state, 2) + + @property + def native_unit_of_measurement(self): + """Return the unit the value is expressed in.""" + return self._unit + @property def state(self): """Return the state of the sensor."""