Skip to content

Commit

Permalink
Auth token fetched with every update
Browse files Browse the repository at this point in the history
  • Loading branch information
macxq committed Oct 22, 2021
1 parent 9fa62f1 commit 21ced46
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
DEFAULT_NAME = "FoxESS"
DEFAULT_VERIFY_SSL = True

SCAN_INTERVAL = timedelta(minutes=2)
SCAN_INTERVAL = timedelta(minutes=1)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
Expand All @@ -65,10 +65,9 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=

methodAuth = "POST"
payloadAuth = {"user": username ,"password":hashedPassword}
verify_ssl = DEFAULT_VERIFY_SSL
headersAuth = {"Content-Type":"application/json;charset=UTF-8","Accept":"application/json, text/plain, */*","lang":"en"}

restAuth = RestData(hass, methodAuth, _ENDPOINT_AUTH, None, headersAuth, None, payloadAuth, verify_ssl)
restAuth = RestData(hass, methodAuth, _ENDPOINT_AUTH, None, headersAuth, None, payloadAuth, DEFAULT_VERIFY_SSL)
await restAuth.async_update()


Expand All @@ -89,17 +88,16 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
headersData = {"Content-Type":"application/json;charset=UTF-8","Accept":"application/json, text/plain, */*","lang":"en", "token":token}


restData = RestData(hass, methodData, _ENDPOINT_DATA+deviceID, None, headersData, None, None, verify_ssl)
restData = RestData(hass, methodData, _ENDPOINT_DATA+deviceID, None, headersData, None, None, DEFAULT_VERIFY_SSL)
await restData.async_update()

if restData.data is None:
_LOGGER.error("Unable to get data from FoxESS Cloud")
return False
else:
_LOGGER.debug("Data fetched "+restData.data)

async_add_entities([FoxESS(restData, name)])
_LOGGER.debug("FoxESS data fetched correcly "+restData.data)

async_add_entities([FoxESS(restData,restAuth, name, deviceID)])

class FoxESS(SensorEntity):
"""Representation of a FoxESS sensor."""
Expand All @@ -108,11 +106,13 @@ class FoxESS(SensorEntity):
_attr_device_class = DEVICE_CLASS_ENERGY
_attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR

def __init__(self, rest, name):
def __init__(self, restData, restAuth, name, deviceID):
"""Initialize a FoxESS sensor."""
_LOGGER.debug("Initing Entity")
self.rest = rest
self.restData = restData
self.restAuth = restAuth
self._attr_name = name
self._attr_unique_id=deviceID
self.pvcoutput = None
self.status = namedtuple(
"status",
Expand Down Expand Up @@ -141,20 +141,40 @@ def extra_state_attributes(self):
}

async def async_update(self):
"""Get the latest data from the PVOutput API and updates the state."""
await self.rest.async_update()
await self.restAuth.async_update()
if self.restAuth.data is None:
_LOGGER.error("Unable to login to FoxESS Cloud - No data recived")
return False

response = json.loads(self.restAuth.data)

if response["result"] is None or response["result"]["token"] is None:
_LOGGER.error("Unable to login to FoxESS Cloud: "+ self.restAuth.data)
return False
else:
_LOGGER.debug("Login succesfull"+ self.restAuth.data)

token = response["result"]["token"]
methodData = "GET"
headersData = {"Content-Type":"application/json;charset=UTF-8","Accept":"application/json, text/plain, */*","lang":"en", "token":token}

self.restData.headersData = headersData


"""Get the latest data from the FoxESS API ;) and updates the state."""
await self.restData.async_update()
self._async_update_from_rest_data()

async def async_added_to_hass(self):
"""Ensure the data from the initial update is reflected in the state."""
self._async_update_from_rest_data()
self._async_update_from_rest_data()

@callback
def _async_update_from_rest_data(self):
"""Update state from the rest data."""

jsonData = json.loads(self.rest.data)
jsonData = json.loads(self.restData.data)
now = datetime.now()

self.pvcoutput = self.status._make([now.strftime("%Y%m%d"),now.strftime("%H:%M"),jsonData["result"]["today"]["generation"],jsonData["result"]["power"]])
_LOGGER.debug(self.pvcoutput)
_LOGGER.debug("FoxESS data fetched "+ self.restData.data)

0 comments on commit 21ced46

Please sign in to comment.