Skip to content

Commit

Permalink
Added support for both old and new api endpoints
Browse files Browse the repository at this point in the history
Modified sensors to try to get them to show up under energy
  • Loading branch information
sockless-coding committed Sep 2, 2021
1 parent 2341699 commit 4458074
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
16 changes: 16 additions & 0 deletions custom_components/garo_wallbox/garo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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']
Expand Down
35 changes: 32 additions & 3 deletions custom_components/garo_wallbox/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
])

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 4458074

Please sign in to comment.