Skip to content

Commit 5847565

Browse files
committed
fix: handle divide by 0
When filter alarm is disabled in mobile app, the value returned by the POST is 0. We will fall back to 9 (months) in that case.
1 parent 4f08e62 commit 5847565

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

custom_components/winix/helpers.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111

1212
from homeassistant.core import HomeAssistant
1313

14-
from .const import DEFAULT_POST_TIMEOUT, LOGGER, WINIX_DOMAIN
14+
from .const import (
15+
DEFAULT_FILTER_ALARM_DURATION,
16+
DEFAULT_POST_TIMEOUT,
17+
LOGGER,
18+
WINIX_DOMAIN,
19+
)
1520
from .device_wrapper import MyWinixDeviceStub
1621

1722

@@ -128,8 +133,18 @@ async def get_filter_alarm_duration(
128133
)
129134

130135
response_json = await resp.json()
136+
137+
# Sample json
131138
# {'resultCode': '200', 'resultMessage': 'SUCCESS', 'filterUsageAlarm': 9}
132-
return int(response_json["filterUsageAlarm"]) * 30 * 24
139+
LOGGER.debug(f"getFilterAlarmInfo: {response_json}")
140+
141+
# Fall back to 9 months if filter alram has been turned off in mobile app in which case we receive this:
142+
# {'resultCode': '200', 'resultMessage': 'SUCCESS', 'filterUsageAlarm': 0}
143+
value = int(response_json["filterUsageAlarm"])
144+
145+
if value == 0:
146+
value = DEFAULT_FILTER_ALARM_DURATION
147+
return value * 30 * 24
133148

134149
@staticmethod
135150
async def get_device_stubs(

0 commit comments

Comments
 (0)