Skip to content

Commit

Permalink
Fix session close (#66)
Browse files Browse the repository at this point in the history
* enable basic python analysis

* add some typing

* await session close

* remove session close (HA does autoclean)

* fix python warning on 'is' operator
  • Loading branch information
krahabb authored Oct 17, 2023
1 parent e203aba commit 41ff018
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.analysis.typeCheckingMode": "basic"
}
47 changes: 15 additions & 32 deletions custom_components/saj_esolar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import asyncio
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from datetime import timedelta
import datetime
import calendar

Expand Down Expand Up @@ -34,6 +33,7 @@
PERCENTAGE,

)
from yarl import URL
CONF_PLANT_ID: Final = "plant_id"
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
Expand All @@ -51,12 +51,12 @@ def add_years(d, years):
try:
return d.replace(year = d.year + years)
except ValueError:
return d + (date(d.year + years, 1, 1) - date(d.year, 1, 1))
return d + (datetime.date(d.year + years, 1, 1) - datetime.date(d.year, 1, 1))

BASE_URL = 'https://fop.saj-electric.com/saj/login'
_LOGGER = logging.getLogger(__name__)

MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5)
MIN_TIME_BETWEEN_UPDATES = datetime.timedelta(minutes=5)

SENSOR_PREFIX = 'esolar '
ATTR_MEASUREMENT = "measurement"
Expand Down Expand Up @@ -127,7 +127,7 @@ def add_years(d, years):
"solarPower",
}

SENSOR_TYPES: Final[tuple[SensorEntityDescription]] = (
SENSOR_TYPES: Final[tuple[SensorEntityDescription, ...]] = (
SensorEntityDescription(
key="nowPower",
name="nowPower",
Expand Down Expand Up @@ -496,11 +496,11 @@ def add_years(d, years):
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_RESOURCES, default=list(SENSOR_LIST)): vol.All(
vol.Required(CONF_RESOURCES, default=list(SENSOR_LIST)): vol.All( # type: ignore
cv.ensure_list, [vol.In(SENSOR_LIST)]
),
vol.Optional(CONF_SENSORS, default="None"): cv.string,
vol.Optional(CONF_PLANT_ID, default=0): cv.positive_int,
vol.Optional(CONF_SENSORS, default="None"): cv.string, # type: ignore
vol.Optional(CONF_PLANT_ID, default=0): cv.positive_int, # type: ignore

}
)
Expand All @@ -524,7 +524,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class SAJeSolarMeterData(object):
"""Handle eSolar object and limit updates."""

def __init__(self, session, username, password, sensors, plant_id):
def __init__(self, session: aiohttp.ClientSession, username, password, sensors, plant_id):
"""Initialize the data object."""

self._session = session
Expand Down Expand Up @@ -630,8 +630,8 @@ async def async_update(self):
plantuid = plantDetails['plantList'][self.plant_id]['plantuid']

deviceSnArr = plantDetails['plantDetail']['snList'][0]
previousChartDay = today - timedelta(days=1)
nextChartDay = today + timedelta(days = 1)
previousChartDay = today - datetime.timedelta(days=1)
nextChartDay = today + datetime.timedelta(days = 1)
chartDay = today.strftime('%Y-%m-%d')
previousChartMonth = add_months(today,-1).strftime('%Y-%m')
nextChartMonth = add_months(today, 1).strftime('%Y-%m')
Expand Down Expand Up @@ -794,7 +794,7 @@ async def async_update(self):


# -Debug- Cookies and Data
_LOGGER.debug(self._session.cookie_jar.filter_cookies("https://fop.saj-electric.com"))
_LOGGER.debug(self._session.cookie_jar.filter_cookies(URL("https://fop.saj-electric.com")))
_LOGGER.debug(self._data)


Expand All @@ -809,7 +809,6 @@ async def async_update(self):

# Clear session and cookies
self._session.cookie_jar.clear()
self._session.close()

@property
def latest_data(self):
Expand All @@ -833,7 +832,7 @@ def __init__(self, description: SensorEntityDescription, data, sensors, plant_id
self.plant_id = plant_id
self._type = self.entity_description.key
self._attr_icon = self.entity_description.icon
self._attr_name = SENSOR_PREFIX + self.entity_description.name
self._attr_name = f"{SENSOR_PREFIX}{self.entity_description.name}"
self._attr_state_class = self.entity_description.state_class
self._attr_native_unit_of_measurement = self.entity_description.native_unit_of_measurement
self._attr_device_class = self.entity_description.device_class
Expand All @@ -857,21 +856,15 @@ async def async_update(self):
if self._type == 'devOnlineNum':
if 'devOnlineNum' in energy['plantDetail']:
if energy['plantDetail']["devOnlineNum"] is not None:
if int(energy['plantDetail']["devOnlineNum"]) is 0:
self._state = "No"
else:
self._state = "Yes"
self._state = "Yes" if int(energy['plantDetail']["devOnlineNum"]) else "No"
if self._type == 'nowPower':
if 'nowPower' in energy['plantDetail']:
if energy['plantDetail']["nowPower"] is not None:
self._state = float(energy['plantDetail']["nowPower"])
if self._type == 'runningState':
if 'runningState' in energy['plantDetail']:
if energy['plantDetail']["runningState"] is not None:
if int(energy['plantDetail']["runningState"]) is 0:
self._state = "No"
else:
self._state = "Yes"
self._state = "Yes" if int(energy['plantDetail']["runningState"]) else "No"
if self._type == 'todayElectricity':
if 'todayElectricity' in energy['plantDetail']:
if energy['plantDetail']["todayElectricity"] is not None:
Expand Down Expand Up @@ -1062,17 +1055,7 @@ async def async_update(self):
if self._type == 'h1Online':
if 'isOnline' in energy["storeDevicePower"]:
if energy["storeDevicePower"]['isOnline'] is not None:
if int(energy['storeDevicePower']["isOnline"]) is 0:
self._state = "No"
else:
self._state = "Yes"
if self._type == 'h1Online':
if 'isOnline' in energy["storeDevicePower"]:
if energy["storeDevicePower"]['isOnline'] is not None:
if int(energy['storeDevicePower']["isOnline"]) is 0:
self._state = "No"
else:
self._state = "Yes"
self._state = "Yes" if int(energy['storeDevicePower']["isOnline"]) else "No"
if self._type == 'outPower':
if 'outPower' in energy["storeDevicePower"]:
if energy["storeDevicePower"]['outPower'] is not None:
Expand Down

0 comments on commit 41ff018

Please sign in to comment.