Skip to content

Commit 2e34f32

Browse files
committed
Fix CCF issue
Signed-off-by: Ian Brown <[email protected]>
1 parent a188ce0 commit 2e34f32

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

.github/workflows/codeql.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: "CodeQL"
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
schedule:
9+
- cron: "0 0 * * 0" # Weekly on Sunday
10+
11+
jobs:
12+
analyze:
13+
name: Analyze
14+
runs-on: ubuntu-latest
15+
permissions:
16+
actions: read
17+
contents: read
18+
security-events: write
19+
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
24+
- name: Initialize CodeQL
25+
uses: github/codeql-action/init@v3
26+
with:
27+
languages: python
28+
29+
- name: Perform CodeQL Analysis
30+
uses: github/codeql-action/analyze@v3
31+

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.7.1",
4+
"version": "1.7.2",
55
"documentation": "https://github.com/zestysoft/sensus_analytics_integration",
66
"dependencies": [],
77
"codeowners": ["@zestysoft"],

custom_components/sensus_analytics/sensor.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .const import DEFAULT_NAME, DOMAIN
1313

1414
CF_TO_GALLON = 7.48052
15+
CF_PER_CCF = 100 # 1 CCF = 100 cubic feet
1516

1617

1718
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities):
@@ -52,32 +53,39 @@ def _convert_usage(self, usage, usage_unit=None):
5253

5354
config_unit_type = self.coordinator.config_entry.data.get("unit_type")
5455

56+
try:
57+
usage_float = float(usage)
58+
except (ValueError, TypeError):
59+
return None
60+
61+
# CF (cubic feet) conversions
5562
if usage_unit == "CF" and config_unit_type == "gal":
56-
try:
57-
return round(float(usage) * CF_TO_GALLON)
58-
except (ValueError, TypeError):
59-
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":
63+
return round(usage_float * CF_TO_GALLON)
64+
if usage_unit == "CF" and config_unit_type == "CCF":
65+
return round(usage_float / CF_PER_CCF, 2)
66+
67+
# GAL (gallons) conversions
68+
if usage_unit == "GAL" and config_unit_type == "gal":
6669
return usage
70+
if usage_unit == "GAL" and config_unit_type == "CCF":
71+
# Convert gallons to cubic feet, then to CCF
72+
return round(usage_float / CF_TO_GALLON / CF_PER_CCF, 2)
73+
6774
return usage
6875

6976
def _get_usage_unit(self):
7077
"""Determine the unit of measurement for usage sensors."""
71-
usage_unit = self.coordinator.data.get("usageUnit")
7278
config_unit_type = self.coordinator.config_entry.data.get("unit_type")
7379

74-
if usage_unit == "CF" and config_unit_type == "gal":
80+
# Return the user's configured unit type
81+
# The _convert_usage method handles the actual value conversion
82+
if config_unit_type == "gal":
7583
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
84+
if config_unit_type == "CCF":
85+
return "CCF"
86+
87+
# Fallback to API-reported unit if config is unexpected
88+
return self.coordinator.data.get("usageUnit")
8189

8290

8391
class DynamicUnitSensorBase(UsageConversionMixin, CoordinatorEntity, SensorEntity):

0 commit comments

Comments
 (0)