diff --git a/custom_components/ups_over_network/config_flow.py b/custom_components/ups_over_network/config_flow.py index d85ee23..425cff3 100644 --- a/custom_components/ups_over_network/config_flow.py +++ b/custom_components/ups_over_network/config_flow.py @@ -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, diff --git a/custom_components/ups_over_network/const.py b/custom_components/ups_over_network/const.py index ad562f8..e09977c 100644 --- a/custom_components/ups_over_network/const.py +++ b/custom_components/ups_over_network/const.py @@ -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"], } diff --git a/custom_components/ups_over_network/sensor.py b/custom_components/ups_over_network/sensor.py index c5de4e8..ffb3bdb 100644 --- a/custom_components/ups_over_network/sensor.py +++ b/custom_components/ups_over_network/sensor.py @@ -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, @@ -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() @@ -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) @@ -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]), } @@ -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