@@ -274,24 +274,36 @@ def native_value(self):
274
274
275
275
def _calculate_cost (self , usage_gallons ):
276
276
"""Calculate the billing cost based on tiers and service fee."""
277
- tier1_gallons = self .coordinator .config_entry .data .get ("tier1_gallons" )
277
+ tier1_gallons = self .coordinator .config_entry .data .get ("tier1_gallons" ) or 0
278
278
tier1_price = self .coordinator .config_entry .data .get ("tier1_price" )
279
- tier2_gallons = self .coordinator .config_entry .data .get ("tier2_gallons" )
280
- tier2_price = self .coordinator .config_entry .data .get ("tier2_price" )
281
- tier3_price = self .coordinator .config_entry .data .get ("tier3_price" )
279
+ tier2_gallons = self .coordinator .config_entry .data .get ("tier2_gallons" ) or 0
280
+ tier2_price = self .coordinator .config_entry .data .get ("tier2_price" ) or 0
281
+ tier3_price = self .coordinator .config_entry .data .get ("tier3_price" ) or 0
282
282
service_fee = self .coordinator .config_entry .data .get ("service_fee" )
283
283
284
284
cost = service_fee
285
285
if usage_gallons is not None :
286
- if usage_gallons <= tier1_gallons :
286
+ if tier1_gallons == 0 :
287
+ # No tier 1 limit, all usage is charged at tier 1 price
287
288
cost += usage_gallons * tier1_price
288
- elif usage_gallons <= tier1_gallons + tier2_gallons :
289
- cost += tier1_gallons * tier1_price
290
- cost += (usage_gallons - tier1_gallons ) * tier2_price
291
- else :
292
- cost += tier1_gallons * tier1_price
293
- cost += tier2_gallons * tier2_price
294
- cost += (usage_gallons - tier1_gallons - tier2_gallons ) * tier3_price
289
+ elif tier2_gallons == 0 :
290
+ # No tier 2 limit, calculate for tier 1 and tier 2
291
+ if usage_gallons <= tier1_gallons :
292
+ cost += usage_gallons * tier1_price
293
+ else :
294
+ cost += tier1_gallons * tier1_price
295
+ cost += (usage_gallons - tier1_gallons ) * tier2_price
296
+ elif tier3_price > 0 :
297
+ # Calculate for all three tiers
298
+ if usage_gallons <= tier1_gallons :
299
+ cost += usage_gallons * tier1_price
300
+ elif usage_gallons <= tier1_gallons + tier2_gallons :
301
+ cost += tier1_gallons * tier1_price
302
+ cost += (usage_gallons - tier1_gallons ) * tier2_price
303
+ else :
304
+ cost += tier1_gallons * tier1_price
305
+ cost += tier2_gallons * tier2_price
306
+ cost += (usage_gallons - tier1_gallons - tier2_gallons ) * tier3_price
295
307
296
308
return round (cost , 2 )
297
309
@@ -317,22 +329,34 @@ def native_value(self):
317
329
318
330
def _calculate_daily_fee (self , usage_gallons ):
319
331
"""Calculate the daily fee based on tiers."""
320
- tier1_gallons = self .coordinator .config_entry .data .get ("tier1_gallons" )
332
+ tier1_gallons = self .coordinator .config_entry .data .get ("tier1_gallons" ) or 0
321
333
tier1_price = self .coordinator .config_entry .data .get ("tier1_price" )
322
- tier2_gallons = self .coordinator .config_entry .data .get ("tier2_gallons" )
323
- tier2_price = self .coordinator .config_entry .data .get ("tier2_price" )
324
- tier3_price = self .coordinator .config_entry .data .get ("tier3_price" )
334
+ tier2_gallons = self .coordinator .config_entry .data .get ("tier2_gallons" ) or 0
335
+ tier2_price = self .coordinator .config_entry .data .get ("tier2_price" ) or 0
336
+ tier3_price = self .coordinator .config_entry .data .get ("tier3_price" ) or 0
325
337
326
338
cost = 0
327
339
if usage_gallons is not None :
328
- if usage_gallons <= tier1_gallons :
340
+ if tier1_gallons == 0 :
341
+ # No tier 1 limit, all usage is charged at tier 1 price
329
342
cost += usage_gallons * tier1_price
330
- elif usage_gallons <= tier1_gallons + tier2_gallons :
331
- cost += tier1_gallons * tier1_price
332
- cost += (usage_gallons - tier1_gallons ) * tier2_price
333
- else :
334
- cost += tier1_gallons * tier1_price
335
- cost += tier2_gallons * tier2_price
336
- cost += (usage_gallons - tier1_gallons - tier2_gallons ) * tier3_price
343
+ elif tier2_gallons == 0 :
344
+ # No tier 2 limit, calculate for tier 1 and tier 2
345
+ if usage_gallons <= tier1_gallons :
346
+ cost += usage_gallons * tier1_price
347
+ else :
348
+ cost += tier1_gallons * tier1_price
349
+ cost += (usage_gallons - tier1_gallons ) * tier2_price
350
+ elif tier3_price > 0 :
351
+ # Calculate for all three tiers
352
+ if usage_gallons <= tier1_gallons :
353
+ cost += usage_gallons * tier1_price
354
+ elif usage_gallons <= tier1_gallons + tier2_gallons :
355
+ cost += tier1_gallons * tier1_price
356
+ cost += (usage_gallons - tier1_gallons ) * tier2_price
357
+ else :
358
+ cost += tier1_gallons * tier1_price
359
+ cost += tier2_gallons * tier2_price
360
+ cost += (usage_gallons - tier1_gallons - tier2_gallons ) * tier3_price
337
361
338
362
return round (cost , 2 )
0 commit comments