Skip to content

Commit 51e4ed9

Browse files
authored
Merge pull request #55 from zestysoft/Add_native_GAL_support
Add native gal support
2 parents 62187f3 + 738d956 commit 51e4ed9

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

custom_components/sensus_analytics/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"domain": "sensus_analytics",
33
"name": "Sensus Analytics Integration",
4-
"version": "1.6.12",
4+
"version": "1.7.0",
55
"documentation": "https://github.com/zestysoft/sensus_analytics_integration",
66
"dependencies": [],
77
"codeowners": ["@zestysoft"],

custom_components/sensus_analytics/sensor.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,43 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e
4242
class UsageConversionMixin:
4343
"""Mixin to provide usage conversion."""
4444

45+
# pylint: disable=too-many-return-statements
4546
def _convert_usage(self, usage, usage_unit=None):
4647
"""Convert usage based on configuration and native unit."""
4748
if usage is None:
4849
return None
4950
if usage_unit is None:
5051
usage_unit = self.coordinator.data.get("usageUnit")
51-
if usage_unit == "CF" and self.coordinator.config_entry.data.get("unit_type") == "gal":
52+
53+
config_unit_type = self.coordinator.config_entry.data.get("unit_type")
54+
55+
if usage_unit == "CF" and config_unit_type == "gal":
5256
try:
5357
return round(float(usage) * CF_TO_GALLON)
5458
except (ValueError, TypeError):
5559
return None
60+
elif usage_unit == "GAL" and config_unit_type == "CF":
61+
try:
62+
return round(float(usage) / CF_TO_GALLON)
63+
except (ValueError, TypeError):
64+
return None
65+
elif usage_unit == "GAL" and config_unit_type == "gal":
66+
return usage
5667
return usage
5768

69+
def _get_usage_unit(self):
70+
"""Determine the unit of measurement for usage sensors."""
71+
usage_unit = self.coordinator.data.get("usageUnit")
72+
config_unit_type = self.coordinator.config_entry.data.get("unit_type")
73+
74+
if usage_unit == "CF" and config_unit_type == "gal":
75+
return "gal"
76+
if usage_unit == "GAL" and config_unit_type == "CF":
77+
return "CF"
78+
if usage_unit == "GAL" and config_unit_type == "gal":
79+
return "gal"
80+
return usage_unit
81+
5882

5983
class DynamicUnitSensorBase(UsageConversionMixin, CoordinatorEntity, SensorEntity):
6084
"""Base class for sensors with dynamic units."""
@@ -72,13 +96,6 @@ def __init__(self, coordinator, entry):
7296
model="Water Meter",
7397
)
7498

75-
def _get_usage_unit(self):
76-
"""Determine the unit of measurement for usage sensors."""
77-
usage_unit = self.coordinator.data.get("usageUnit")
78-
if usage_unit == "CF" and self.coordinator.config_entry.data.get("unit_type") == "gal":
79-
return "gal"
80-
return usage_unit
81-
8299
@property
83100
def native_unit_of_measurement(self):
84101
"""Return the unit of measurement."""

0 commit comments

Comments
 (0)