Skip to content

Commit

Permalink
login before every request
Browse files Browse the repository at this point in the history
  • Loading branch information
thematrixdev committed Sep 23, 2024
1 parent 034a101 commit 89859db
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion custom_components/clp/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "10.6.5",
"version": "10.6.6",
"domain": "clp",
"name": "CLP",
"documentation": "https://github.com/thematrixdev/home-assistant-clp",
Expand Down
56 changes: 41 additions & 15 deletions custom_components/clp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,19 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None,
) -> None:
session = aiohttp_client.async_get_clientsession(hass)
csrf_token = await get_csrf_token(session, config.get(CONF_TIMEOUT))
await login(session, config.get(CONF_USERNAME), config.get(CONF_PASSWORD), csrf_token, config.get(CONF_TIMEOUT))

hass.data[DOMAIN] = {
'username': config.get(CONF_USERNAME),
'password': config.get(CONF_PASSWORD),
'session': session,
}

async_add_entities(
[
CLPSensor(
sensor_type='main',
session=session,
csrf_token=csrf_token,
csrf_token=None,
name=config.get(CONF_NAME),
timeout=config.get(CONF_TIMEOUT),
retry_delay=config.get(CONF_RETRY_DELAY),
Expand All @@ -114,7 +118,7 @@ async def async_setup_platform(
CLPSensor(
sensor_type='renewable_energy',
session=session,
csrf_token=csrf_token,
csrf_token=None,
name=config.get(CONF_RES_NAME),
timeout=config.get(CONF_TIMEOUT),
retry_delay=config.get(CONF_RETRY_DELAY),
Expand Down Expand Up @@ -231,6 +235,7 @@ def __init__(
self._attr_state_class = SensorStateClass.TOTAL

self._state_data_type = None
self.error = None

@property
def state_class(self) -> SensorStateClass | str | None:
Expand All @@ -244,6 +249,7 @@ def name(self) -> str | None:
def extra_state_attributes(self) -> dict:
attr = {
"state_data_type": self._state_data_type,
"error": None,
}

if hasattr(self, '_account'):
Expand Down Expand Up @@ -274,6 +280,15 @@ async def async_update(self) -> None:
try:
dates = get_dates(self._timezone)

self._csrf_token = await get_csrf_token(self._session, self._timeout)
await login(
self._session,
self.hass.data[DOMAIN]['username'], # Use stored username/password from hass.data
self.hass.data[DOMAIN]['password'],
self._csrf_token,
self._timeout
)

if self._sensor_type == 'main':
if self._get_acct:
_LOGGER.debug("CLP ACCOUNT")
Expand Down Expand Up @@ -341,10 +356,12 @@ async def async_update(self) -> None:
if self._type == '' or self._type.upper() == 'BIMONTHLY':
self._state_data_type = 'BIMONTHLY'
self._attr_native_value = data['results'][0]['TOT_KWH']
self._attr_last_reset = datetime.datetime.strptime(data['results'][0]['PERIOD_LABEL'], '%Y%m%d%H%M%S')
self._attr_last_reset = datetime.datetime.strptime(data['results'][0]['PERIOD_LABEL'],
'%Y%m%d%H%M%S')
if self._get_bill:
self._billed = {
"period": datetime.datetime.strptime(data['results'][0]['PERIOD_LABEL'], '%Y%m%d%H%M%S'),
"period": datetime.datetime.strptime(data['results'][0]['PERIOD_LABEL'],
'%Y%m%d%H%M%S'),
"kwh": data['results'][0]['TOT_KWH'],
"cost": data['results'][0]['TOT_COST'],
}
Expand Down Expand Up @@ -398,7 +415,8 @@ async def async_update(self) -> None:
consumed_end = datetime.datetime.strptime(data['currentEndDate'], '%Y%m%d%H%M%S')

if data['projectedStartDate']:
estimation_start = datetime.datetime.strptime(data['projectedStartDate'], '%Y%m%d%H%M%S')
estimation_start = datetime.datetime.strptime(data['projectedStartDate'],
'%Y%m%d%H%M%S')

if data['projectedEndDate']:
estimation_end = datetime.datetime.strptime(data['projectedEndDate'], '%Y%m%d%H%M%S')
Expand Down Expand Up @@ -452,7 +470,8 @@ async def async_update(self) -> None:
if self._type == '' or self._type.upper() == 'DAILY':
self._state_data_type = 'DAILY'
self._attr_native_value = data['results'][-1]['KWH_TOTAL']
self._attr_last_reset = datetime.datetime.strptime(data['results'][-1]['START_DT'], '%Y%m%d%H%M%S')
self._attr_last_reset = datetime.datetime.strptime(data['results'][-1]['START_DT'],
'%Y%m%d%H%M%S')

if self._get_daily:
self._daily = []
Expand Down Expand Up @@ -501,7 +520,8 @@ async def async_update(self) -> None:
if self._type == '' or self._type.upper() == 'HOURLY':
self._state_data_type = 'HOURLY'
self._attr_native_value = data['results'][-1]['KWH_TOTAL']
self._attr_last_reset = datetime.datetime.strptime(data['results'][-1]['START_DT'], '%Y%m%d%H%M%S')
self._attr_last_reset = datetime.datetime.strptime(data['results'][-1]['START_DT'],
'%Y%m%d%H%M%S')

if self._get_hourly:
self._hourly = []
Expand Down Expand Up @@ -547,7 +567,8 @@ async def async_update(self) -> None:
if self._type == '' or self._type.upper() == 'BIMONTHLY':
self._state_data_type = 'BIMONTHLY'
self._attr_native_value = float(data['ConsumptionData'][-1]['KWHTotal'])
self._attr_last_reset = datetime.datetime.strptime(data['ConsumptionData'][-1]['Enddate'], '%Y%m%d%H%M%S')
self._attr_last_reset = datetime.datetime.strptime(
data['ConsumptionData'][-1]['Enddate'], '%Y%m%d%H%M%S')

if self._get_bill:
self._bills = []
Expand Down Expand Up @@ -590,7 +611,8 @@ async def async_update(self) -> None:
if row['ValidateStatus'] == 'Y':
self._state_data_type = 'DAILY'
self._attr_native_value = float(row['KWHTotal'])
self._attr_last_reset = datetime.datetime.strptime(row['Startdate'], '%Y%m%d%H%M%S')
self._attr_last_reset = datetime.datetime.strptime(row['Startdate'],
'%Y%m%d%H%M%S')
break

if self._get_daily:
Expand Down Expand Up @@ -644,11 +666,13 @@ async def async_update(self) -> None:

if data['ConsumptionData']:
if i == 0 and (self._type == '' or self._type.upper() == 'HOURLY'):
for row in sorted(data['ConsumptionData'], key=lambda x: x['Startdate'], reverse=True):
for row in sorted(data['ConsumptionData'], key=lambda x: x['Startdate'],
reverse=True):
if row['ValidateStatus'] == 'Y':
self._state_data_type = 'HOURLY'
self._attr_native_value = float(row['KWHTotal'])
self._attr_last_reset = datetime.datetime.strptime(row['Startdate'], '%Y%m%d%H%M%S')
self._attr_last_reset = datetime.datetime.strptime(row['Startdate'],
'%Y%m%d%H%M%S')
break

if self._get_hourly:
Expand All @@ -665,5 +689,7 @@ async def async_update(self) -> None:

_LOGGER.debug("CLP END")
except Exception as e:
_LOGGER.debug(e)
async_call_later(self.hass, self._retry_delay, self.schedule_update_ha_state)
_LOGGER.error(f"Error updating sensor {self._name}: {e}", exc_info=True)
self._attr_native_value = None
self.error = e
async_call_later(self.hass, self._retry_delay, self.async_update)
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "clp",
"name": "CLP",
"render_readme": true
}

0 comments on commit 89859db

Please sign in to comment.