Skip to content

Pet weight: keep the decimal when writing to the API - #288

Merged
Jezza34000 merged 2 commits into
Jezza34000:mainfrom
trunten82:fix/pet-weight-decimals
Aug 3, 2026
Merged

Pet weight: keep the decimal when writing to the API#288
Jezza34000 merged 2 commits into
Jezza34000:mainfrom
trunten82:fix/pet-weight-decimals

Conversation

@trunten82

Copy link
Copy Markdown

Problem

number.<pet>_peso cannot be set to a fractional weight. The descriptor declares
native_step=0.1, but the write casts to int:

https://github.com/Jezza34000/homeassistant_petkit/blob/main/custom_components/petkit/number.py#L238

native_step=0.1,
...
action=lambda api, device, value: api.send_api_request(
    device.id, PetCommand.PET_UPDATE_SETTING, {"weight": int(value)}
),

So setting 3.2 kg sends {"weight": 3}, the entity reads back its old value, and
it looks to the user as if the write silently failed. For a 3-4 kg cat that is a
5-15% error, and 0.1 kg is exactly the resolution a weight trend needs to be
useful. Present on main and in 1.25.0 - 1.27.0.

Fix

Round to one decimal, matching native_step, instead of truncating.

Verification

Against api.eu-pet.com/latest/pet/updatepetprops with a real account on 1.27.0,
the API accepts a float and echoes it back:

DEBUG [pypetkitapi.client] Control API device=pet id=101401316
      action=pet_update_setting param={'weight': 3.2}
DEBUG [pypetkitapi.client] Command execution success, API response :
      {'id': '101401316', 'name': 'Tea', ..., 'weight': 3.2,
       'weightLabel': 'Normal', 'updatedAt': '2026-07-28T22:32:20.000+0000'}

Confirmed in the PetKit app afterwards (7.1 lb with the app set to imperial,
= 3.2 kg). Three cats set to 3.3 / 3.2 / 4.3 kg, all correct.

Separate issue found while verifying, not addressed here

Worth a look independently: after a successful write the value does not appear
in Home Assistant. Every subsequent coordinator refresh kept reporting the old
weight from the account/family listing, even minutes later and after
homeassistant.update_entity, while the pet record server-side had already been
updated. Only reloading the config entry (a fresh login) surfaced the new value.

The pet data appears to come from the account listing rather than a pet-detail
read, and that listing seems to be served stale after updatepetprops. Happy to
open a separate issue with logs if useful.

The Pet weight number declares native_step=0.1, but the write casts the value
to int, so every fractional weight is silently discarded: setting 3.2 kg sends
{"weight": 3} and the entity reads back its previous value. For a 3-4 kg cat
that is a 5-15% error, and 0.1 kg steps are exactly the resolution a weight
trend needs.

The API accepts a float. Verified against api.eu-pet.com/latest/pet/updatepetprops
with a real account: {"weight": 3.2} returns

  {'id': '101401316', 'name': 'Tea', ..., 'weight': 3.2, 'weightLabel': 'Normal',
   'updatedAt': '2026-07-28T22:32:20.000+0000'}

and the value shows correctly in the PetKit app afterwards. Rounded to one
decimal to match native_step rather than passed through raw.
@trunten82
trunten82 requested a review from Jezza34000 as a code owner July 28, 2026 22:38
@sonarqubecloud

Copy link
Copy Markdown

@Jezza34000

Copy link
Copy Markdown
Owner

Hi @trunten82
Thanks for reporting and fixing it !
Tested OK on my side & CI OK
Will be released on next version 👍

@Jezza34000
Jezza34000 merged commit e2a796a into Jezza34000:main Aug 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants