Skip to content

Feeder.device_records property is not populated correctly for PetKit Fresh Element #4

@Jellymath

Description

@Jellymath

Hi. I wanted to try using homeassistant_petkit integration with dispenser-schedule-card, however, I found out that required sensor is not populated for my feeder

I tried to investigate a bit why that's the case and found that Feeder.device_records is not filled with the required info due to unexpected endpoint response format

Here are two responses that I got today (response value from _fetch_device_data, FeederRecord data class, FEEDER device type)

  1. Breakfast success, Dinner pending:
[
  {
    'amount': 60,
    'day': 20250503,
    'device_id': 0,
    'items': [
      {
        'amount': 40,
        'id': 's36000',
        'name': 'Breakfast',
        'petAmount': [
          {
            'amount': 40,
            'petId': '100112726'
          }
        ],
        'src': 1,
        'state': {
          'completedAt': '2025-05-03T08:00:10.000+0000',
          'realAmount': 40
        },
        'status': 0,
        'time': 36000
      },
      {
        'amount': 20,
        'id': 's72000',
        'name': 'Dinner',
        'petAmount': [
          {
            'amount': 40,
            'petId': '100112726'
          }
        ],
        'src': 1,
        'status': 0,
        'time': 72000
      }
    ],
    'realAmount': 40
  }
]
  1. Breakfast error (the feeder is empty), Dinner pending:
[
  {
    'amount': 60,
    'day': 20250503,
    'device_id': 0,
    'items': [
      {
        'amount': 40,
        'id': 's36000',
        'name': 'Breakfast',
        'petAmount': [
          {
            'amount': 40,
            'petId': '100112727'
          }
        ],
        'src': 1,
        'state': {
          'completedAt': '2025-05-03T08:00:29.000+0000',
          'errCode': 'food-0',
          'errMsg': 'Food low.',
          'realAmount': 0
        },
        'status': 0,
        'time': 36000
      },
      {
        'amount': 20,
        'id': 's72000',
        'name': 'Dinner',
        'petAmount': [
          {
            'amount': 40,
            'petId': '100112727'
          }
        ],
        'src': 1,
        'status': 0,
        'time': 72000
      }
    ],
    'realAmount': 0
  }
]

While the structure is similar to the expected one, there are some differences:

  • There is no top-level feed property, we start with an array
  • device_id is in snake_case while camelCase is expected
  • petAmount is not parsed (probably not necessary tho)
  • errCode in state is string while int is expected
  • errMsg in state is not parsed

I tried doing some quick fixes like changing _fetch_device_data to have something like:

        if device_type == FEEDER and data_class == FeederRecord:
            device_data = FeederRecord(**{'feed': response})
        elif isinstance(response, list):
            device_data = [data_class(**item) for item in response]
        elif isinstance(response, dict):
            device_data = data_class(**response)

But this fails for records with errors like the second one

Ideally it would be nice to separate endpoint response types from library model types, but it's not that easy to do without knowing other endpoint formats and the required library model. So I decided to just open the issue first :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions