Skip to content

Commit a522525

Browse files
committed
feat: add battery level support
1 parent e5dbee5 commit a522525

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

custom_components/ups_over_network/config_flow.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
vol.Required(CONF_RESOURCES): vol.All(
2323
cv.ensure_list, [vol.In(SENSOR_DEFINITIONS.keys())]
2424
),
25+
vol.Optional("low_battery_voltage", default=24): cv.positive_int,
26+
vol.Optional("full_battery_voltage", default=27): cv.positive_int,
2527
vol.Optional(CONF_SCAN_INTERVAL, default=timedelta(seconds=30)): cv.time_period,
2628
},
2729
extra=vol.ALLOW_EXTRA,

custom_components/ups_over_network/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
"frequency": ["Frequency", UnitOfFrequency.HERTZ, "mdi:current-ac"],
1616
"battery_voltage": ["Battery Voltage", UnitOfElectricPotential.VOLT, "mdi:battery"],
1717
"temperature": ["Temperature", UnitOfTemperature.CELSIUS, "mdi:thermometer"],
18+
"battery_level": ["Battery Level", PERCENTAGE, "mdi:battery"],
1819
}

custom_components/ups_over_network/sensor.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
3434
ups_protocol = config[CONF_PROTOCOL]
3535
resources = config[CONF_RESOURCES]
3636
scan_interval = config.get(CONF_SCAN_INTERVAL, timedelta(seconds=30))
37+
low_battery_voltage = config["low_battery_voltage"]
38+
full_battery_voltage = config["full_battery_voltage"]
3739

3840
coordinator = UPSDataUpdateCoordinator(
3941
hass,
@@ -43,6 +45,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
4345
ups_host=ups_host,
4446
ups_port=ups_port,
4547
ups_protocol=ups_protocol,
48+
low_battery_voltage=low_battery_voltage,
49+
full_battery_voltage=full_battery_voltage,
4650
)
4751

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

6670
def __init__(
67-
self, hass, logger, name, update_interval, ups_host, ups_port, ups_protocol
71+
self,
72+
hass,
73+
logger,
74+
name,
75+
update_interval,
76+
ups_host,
77+
ups_port,
78+
ups_protocol,
79+
low_battery_voltage,
80+
full_battery_voltage,
6881
):
6982
"""Initialize."""
7083
self.ups_host = ups_host
7184
self.ups_port = ups_port
7285
self.ups_protocol = ups_protocol
86+
self.low_battery_voltage = low_battery_voltage
87+
self.full_battery_voltage = full_battery_voltage
7388

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

@@ -101,6 +116,15 @@ async def _async_update_data_megatec_q1(self, reader, writer):
101116
"load": float(values[3]),
102117
"frequency": float(values[4]),
103118
"battery_voltage": float(values[5]),
119+
"battery_level": max(
120+
0,
121+
min(
122+
1,
123+
(float(values[5]) - self.low_battery_voltage)
124+
/ (self.full_battery_voltage - self.low_battery_voltage),
125+
),
126+
)
127+
* 100,
104128
"temperature": float(values[6]),
105129
}
106130

@@ -161,6 +185,12 @@ def state(self):
161185
@property
162186
def icon(self):
163187
"""Icon to use in the frontend, if any."""
188+
if self._sensor_type == "battery_level":
189+
if self.state == 100:
190+
return "mdi:battery"
191+
if self.state < 10:
192+
return "mdi:battery-alert-variant-outline"
193+
return f"mdi:battery-{int(self.state / 10) * 10}"
164194
return self._sensor_icon
165195

166196
@property

0 commit comments

Comments
 (0)