Skip to content

Commit

Permalink
feat: add battery level support
Browse files Browse the repository at this point in the history
  • Loading branch information
tinymins committed Dec 16, 2024
1 parent e5dbee5 commit a522525
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions custom_components/ups_over_network/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
vol.Required(CONF_RESOURCES): vol.All(
cv.ensure_list, [vol.In(SENSOR_DEFINITIONS.keys())]
),
vol.Optional("low_battery_voltage", default=24): cv.positive_int,
vol.Optional("full_battery_voltage", default=27): cv.positive_int,
vol.Optional(CONF_SCAN_INTERVAL, default=timedelta(seconds=30)): cv.time_period,
},
extra=vol.ALLOW_EXTRA,
Expand Down
1 change: 1 addition & 0 deletions custom_components/ups_over_network/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
"frequency": ["Frequency", UnitOfFrequency.HERTZ, "mdi:current-ac"],
"battery_voltage": ["Battery Voltage", UnitOfElectricPotential.VOLT, "mdi:battery"],
"temperature": ["Temperature", UnitOfTemperature.CELSIUS, "mdi:thermometer"],
"battery_level": ["Battery Level", PERCENTAGE, "mdi:battery"],
}
32 changes: 31 additions & 1 deletion custom_components/ups_over_network/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
ups_protocol = config[CONF_PROTOCOL]
resources = config[CONF_RESOURCES]
scan_interval = config.get(CONF_SCAN_INTERVAL, timedelta(seconds=30))
low_battery_voltage = config["low_battery_voltage"]
full_battery_voltage = config["full_battery_voltage"]

coordinator = UPSDataUpdateCoordinator(
hass,
Expand All @@ -43,6 +45,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
ups_host=ups_host,
ups_port=ups_port,
ups_protocol=ups_protocol,
low_battery_voltage=low_battery_voltage,
full_battery_voltage=full_battery_voltage,
)

await coordinator.async_config_entry_first_refresh()
Expand All @@ -64,12 +68,23 @@ class UPSDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching UPS data."""

def __init__(
self, hass, logger, name, update_interval, ups_host, ups_port, ups_protocol
self,
hass,
logger,
name,
update_interval,
ups_host,
ups_port,
ups_protocol,
low_battery_voltage,
full_battery_voltage,
):
"""Initialize."""
self.ups_host = ups_host
self.ups_port = ups_port
self.ups_protocol = ups_protocol
self.low_battery_voltage = low_battery_voltage
self.full_battery_voltage = full_battery_voltage

super().__init__(hass, logger, name=name, update_interval=update_interval)

Expand Down Expand Up @@ -101,6 +116,15 @@ async def _async_update_data_megatec_q1(self, reader, writer):
"load": float(values[3]),
"frequency": float(values[4]),
"battery_voltage": float(values[5]),
"battery_level": max(
0,
min(
1,
(float(values[5]) - self.low_battery_voltage)
/ (self.full_battery_voltage - self.low_battery_voltage),
),
)
* 100,
"temperature": float(values[6]),
}

Expand Down Expand Up @@ -161,6 +185,12 @@ def state(self):
@property
def icon(self):
"""Icon to use in the frontend, if any."""
if self._sensor_type == "battery_level":
if self.state == 100:
return "mdi:battery"
if self.state < 10:
return "mdi:battery-alert-variant-outline"
return f"mdi:battery-{int(self.state / 10) * 10}"
return self._sensor_icon

@property
Expand Down

0 comments on commit a522525

Please sign in to comment.