Skip to content

Commit 31c51ce

Browse files
authored
Do not double IKEA battery percentage by default (#3176)
1 parent 3e8d14d commit 31c51ce

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

tests/test_ikea.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ def mock_read(attributes, manufacturer=None):
159159
@pytest.mark.parametrize(
160160
"firmware, pct_device, pct_correct, expected_pct_updates, expect_log_warning",
161161
(
162-
("1.0.024", 50, 100, 1, False), # old firmware, doubling
163-
("2.3.075", 50, 100, 1, False), # old firmware, doubling
164-
("2.4.5", 50, 50, 2, False), # new firmware, no doubling
165-
("3.0.0", 50, 50, 2, False), # new firmware, no doubling
166-
("24.4.5", 50, 50, 2, False), # new firmware, no doubling
167-
("invalid_fw_string_1", 50, 50, 2, False), # treated as new, no doubling
168-
("invalid.fw.string.2", 50, 50, 2, True), # treated as new, no doubling + log
169-
("", 50, 100, 1, False), # treated as old fw, doubling
162+
("1.0.024", 50, 100, 2, False), # old firmware, doubling
163+
("2.3.075", 50, 100, 2, False), # old firmware, doubling
164+
("2.4.5", 50, 50, 1, False), # new firmware, no doubling
165+
("3.0.0", 50, 50, 1, False), # new firmware, no doubling
166+
("24.4.5", 50, 50, 1, False), # new firmware, no doubling
167+
("invalid_fw_string_1", 50, 50, 1, False), # treated as new, no doubling
168+
("invalid.fw.string.2", 50, 50, 1, True), # treated as new, no doubling + log
169+
("", 50, 50, 1, False), # treated as new fw, no doubling
170170
),
171171
)
172172
async def test_double_power_config_firmware(
@@ -206,10 +206,10 @@ def mock_read(attributes, manufacturer=None):
206206
)
207207

208208
with p1 as mock_task, p2 as request_mock:
209-
# update battery percentage with no firmware in attr cache, check pct doubled for now
209+
# update battery percentage with no firmware in attr cache, check pct not doubled for now
210210
power_cluster.update_attribute(battery_pct_id, pct_device)
211211
assert len(power_listener.attribute_updates) == 1
212-
assert power_listener.attribute_updates[0] == (battery_pct_id, pct_device * 2)
212+
assert power_listener.attribute_updates[0] == (battery_pct_id, pct_device)
213213

214214
# but also check that sw_build_id read is requested in the background for next update
215215
assert mock_task.call_count == 1

zhaquirks/ikea/__init__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ def _is_firmware_new(self):
215215
# get sw_build_id from attribute cache if available
216216
sw_build_id = self.endpoint.basic.get(Basic.AttributeDefs.sw_build_id.id)
217217

218-
# sw_build_id is not cached or empty, so we consider it old firmware for now
218+
# sw_build_id is not cached or empty, so we consider it new firmware for now
219219
if not sw_build_id:
220-
return False
220+
return True
221221

222222
# split sw_build_id into parts to check for new firmware
223223
split_fw_version = sw_build_id.split(".")
@@ -247,19 +247,19 @@ async def _read_fw_and_update_battery_pct(self, reported_battery_pct):
247247
# read sw_build_id from device
248248
await self.endpoint.basic.read_attributes([Basic.AttributeDefs.sw_build_id.id])
249249

250-
# check if sw_build_id was read successfully and new firmware is installed
251-
# if so, update cache with reported battery percentage (non-doubled)
252-
if self._is_firmware_new():
250+
# check if sw_build_id was read successfully and old firmware is installed
251+
# if so, update cache with reported battery percentage (doubled)
252+
if not self._is_firmware_new():
253253
self._update_attribute(
254254
PowerConfiguration.AttributeDefs.battery_percentage_remaining.id,
255-
reported_battery_pct,
255+
reported_battery_pct * 2,
256256
)
257257

258258
def _update_attribute(self, attrid, value):
259-
"""Update attribute to double battery percentage if firmware is old/unknown.
259+
"""Update attribute to double battery percentage if firmware is old.
260260
261261
If the firmware version is unknown, a background task to read the firmware version is also started,
262-
but the percentage is also doubled for now then, as that task happens asynchronously.
262+
but the percentage is not doubled for now then, as that task happens asynchronously.
263263
"""
264264
if attrid == PowerConfiguration.AttributeDefs.battery_percentage_remaining.id:
265265
# if sw_build_id is not cached, create task to read from device, since it should be awake now
@@ -269,9 +269,9 @@ def _update_attribute(self, attrid, value):
269269
):
270270
self.create_catching_task(self._read_fw_and_update_battery_pct(value))
271271

272-
# double percentage if the firmware is old or unknown
273-
# the coroutine above will not have executed yet if the firmware is unknown,
274-
# so we double for now in that case too, and it updates again later if our doubling was wrong
272+
# double percentage if the firmware is confirmed old
273+
# The coroutine above will not have executed yet if the firmware is unknown,
274+
# so we don't double for now. The coro doubles the value later if needed.
275275
if not self._is_firmware_new():
276276
value = value * 2
277277
super()._update_attribute(attrid, value)

0 commit comments

Comments
 (0)