Skip to content

Commit

Permalink
refactor: Battery State standby changed to idle
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrapan committed Aug 8, 2024
1 parent 57bcaac commit cde1c6a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
32 changes: 16 additions & 16 deletions custom_components/solarman/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class Inverter(PySolarmanV5AsyncWrapper):
def __init__(self, address, serial, port, mb_slave_id, passthrough):
super().__init__(address, serial, port, mb_slave_id, passthrough)
self._is_reading = 0
self.status_updated = datetime.now()
self.status_interval = 0
self.status = -1
self.state_updated = datetime.now()
self.state_interval = 0
self.state = -1
self.auto_reconnect = AUTO_RECONNECT
self.manufacturer = "Solarman"
self.model = None
Expand Down Expand Up @@ -84,14 +84,14 @@ async def load(self, name, mac, path, file):
_LOGGER.debug(self.device_info)

def available(self):
return self.status > -1
return self.state > -1

async def async_connect(self, loud = True) -> None:
if not self.reader_task:
if loud:
_LOGGER.info(f"Connecting to {self.address}:{self.port}")
await self.connect()
elif not self.status > 0:
elif not self.state > 0:
await self.reconnect()

async def async_disconnect(self, loud = True) -> None:
Expand All @@ -106,7 +106,7 @@ async def async_disconnect(self, loud = True) -> None:

async def async_shutdown(self, loud = True) -> None:
self._is_reading = 0
self.status = -1
self.state = -1
await self.async_disconnect(loud)

async def async_read(self, params, code, start, end) -> None:
Expand All @@ -128,8 +128,8 @@ def get_sensors(self):
return params.get_sensors()
return []

def get_connection_status(self):
if self.status > 0:
def get_connection_state(self):
if self.state > 0:
return "Connected"
return "Disconnected"

Expand All @@ -139,21 +139,21 @@ def get_result(self, middleware = None):
result = middleware.get_result() if middleware else {}

if len(result) > 0:
_LOGGER.debug(f"Returning new values to the Coordinator. [Previous State: {self.get_connection_status()} ({self.status})]")
_LOGGER.debug(f"Returning new values to the Coordinator. [Previous State: {self.get_connection_state()} ({self.state})]")
now = datetime.now()
self.status_interval = now - self.status_updated
self.status_updated = now
self.status = 1
self.state_interval = now - self.state_updated
self.state_updated = now
self.state = 1

return result

async def async_get_failed(self, message = None):
_LOGGER.debug(f"Request failed. [Previous State: {self.get_connection_status()} ({self.status})]")
self.status = 0 if self.status == 1 else -1
_LOGGER.debug(f"Request failed. [Previous State: {self.get_connection_state()} ({self.state})]")
self.state = 0 if self.state == 1 else -1

await self.async_disconnect()

if message and self.status == -1:
if message and self.state == -1:
raise UpdateFailed(message)

async def async_get(self, runtime = 0):
Expand Down Expand Up @@ -201,7 +201,7 @@ async def async_get(self, runtime = 0):
await self.async_get_failed(f"Querying {self.serial} at {self.address}:{self.port} failed.")

except TimeoutError:
last_state = self.status
last_state = self.state
await self.async_get_failed()
if last_state < 1:
raise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ parameters:
uom: ""
scale: 1
rule: 0
options: ["charging", "standby", "discharging"]
options: ["charging", "idle", "discharging"]
icon: "mdi:battery"

# Battery - The current of battery 1 is S16bit (low 16 bits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,7 @@ parameters:
uom: ""
scale: 1
rule: 0
options: ["charging", "standby", "discharging"]
options: ["charging", "idle", "discharging"]
icon: "mdi:battery"

# Battery - The current of battery 1 is S16bit (low 16 bits)
Expand Down
8 changes: 4 additions & 4 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def available(self) -> bool:
return True

def update(self):
self._attr_state = self.coordinator.inverter.get_connection_status()
self._attr_extra_state_attributes["updated"] = self.coordinator.inverter.status_updated.strftime("%m/%d/%Y, %H:%M:%S")
self._attr_state = self.coordinator.inverter.get_connection_state()
self._attr_extra_state_attributes["updated"] = self.coordinator.inverter.state_updated.strftime("%m/%d/%Y, %H:%M:%S")

class SolarmanInterval(SolarmanDiagnosticEntity):
def __init__(self, coordinator, sensor):
Expand All @@ -96,7 +96,7 @@ def available(self) -> bool:
return self._attr_state > 0

def update(self):
self._attr_state = self.coordinator.inverter.status_interval.total_seconds()
self._attr_state = self.coordinator.inverter.state_interval.total_seconds()

class SolarmanSensor(SolarmanEntity):
def __init__(self, coordinator, sensor, battery_nominal_voltage, battery_life_cycle_rating):
Expand Down Expand Up @@ -165,7 +165,7 @@ def update(self):
case "Battery State":
battery_power = self.get_data("Battery Power", None)
if battery_power:
self._attr_state = "discharging" if battery_power > 50 else "charging" if battery_power < -50 else "standby"
self._attr_state = "discharging" if battery_power > 50 else "charging" if battery_power < -50 else "idle"
case "Today Battery Life Cycles":
today_battery_charge = self.get_data("Today Battery Charge", None)
if today_battery_charge == 0:
Expand Down

0 comments on commit cde1c6a

Please sign in to comment.