Skip to content

Commit

Permalink
Use battery serial number when selected sensor h1 (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaesbit authored Aug 9, 2024
1 parent 2cca9a3 commit 5ded708
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions custom_components/saj_esolar/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ def add_years(d, years):
BASE_URL = 'https://fop.saj-electric.com/saj/login'
_LOGGER = logging.getLogger(__name__)

DEVICE_TYPES = {
"Inverter": 0,
"Meter": 1, # TODO: Pending to confirm
"Battery": 2,
0: "Inverter",
1: "Meter", # TODO: Pending to confirm
2: "Battery",
}

MIN_TIME_BETWEEN_UPDATES = datetime.timedelta(minutes=5)

SENSOR_PREFIX = 'esolar '
Expand Down Expand Up @@ -274,7 +283,7 @@ def add_years(d, years):
icon="mdi:solar-panel",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
),
),
SensorEntityDescription(
key="pvElec",
name="pvElec",
Expand Down Expand Up @@ -504,7 +513,7 @@ def add_years(d, years):
vol.Optional("provider_path", default="saj"):cv.string,
vol.Optional("provider_protocol", default="https"):cv.string,
vol.Optional("provider_ssl", default=True):cv.boolean,


}
)
Expand Down Expand Up @@ -532,7 +541,7 @@ def __init__(self,host,path,protocol):
self.host=host
self.path=path
self.protocol=protocol

def getBaseDomain(self):
return f"{self.protocol}://{self.host}"

Expand All @@ -541,7 +550,7 @@ def getBaseUrl(self):

def getLoginUrl(self):
return f"{self.getBaseUrl()}/login"



class SAJeSolarMeterData(object):
Expand Down Expand Up @@ -634,7 +643,6 @@ async def async_update(self):
plantInfo = await response2.json()
plantuid = plantInfo['plantList'][self.plant_id]['plantuid']


# Get API Plant Solar Details
url3 = f"{self._provider.getBaseUrl()}/monitor/site/getPlantDetailInfo"
payload3= f"plantuid={plantuid}&clientDate={clientDate}"
Expand All @@ -649,11 +657,33 @@ async def async_update(self):
#_LOGGER.error(f"PlantDetails: {plantDetails}")
plantDetails.update(plantInfo)

devicesInfoUrl = f"{self._provider.getBaseUrl()}/cloudMonitor/device/findDevicePageList"
devicesInfoPayload = f"officeId=&pageNo=&pageSize=&orderName=1&orderType=2&plantuid={plantuid}&deviceStatus=&localDate=&localMonth="
deviceInfoReponse = await self._session.post(
devicesInfoUrl, headers=headers, data=devicesInfoPayload
)
if deviceInfoReponse.status != 200:
_LOGGER.error(
"{deviceInfoReponse.url} returned {deviceInfoReponse.status}"
)
return

# getPlantDetailChart2
plantuid = plantDetails['plantList'][self.plant_id]['plantuid']
devicesInfoData = await deviceInfoReponse.json()
plantDetails.update(devicesInfoData)
if self.sensors == "h1":
deviceSnArr = next(
(
item['devicesn']
for item in plantDetails["list"]
if item["type"] == DEVICE_TYPES["Battery"]
),
plantDetails["plantDetail"]["snList"][0],
)
else:
deviceSnArr = plantDetails["plantDetail"]["snList"][0]

deviceSnArr = plantDetails['plantDetail']['snList'][0]

# getPlantDetailChart2
previousChartDay = today - datetime.timedelta(days=1)
nextChartDay = today + datetime.timedelta(days = 1)
chartDay = today.strftime('%Y-%m-%d')
Expand Down Expand Up @@ -727,7 +757,6 @@ async def async_update(self):
# -Debug- Sec module serial number
_LOGGER.debug(moduleSn)


# findDevicePageList
url_findDevicePageList = f"{self._provider.getBaseUrl()}/cloudMonitor/device/findDevicePageList"

Expand Down Expand Up @@ -1205,4 +1234,4 @@ async def async_update(self):


# -Debug- adding sensor
_LOGGER.debug(f"Device: {self._type} State: {self._state}")
_LOGGER.debug(f"Device: {self._type} State: {self._state}")

0 comments on commit 5ded708

Please sign in to comment.