Skip to content

[Library Bug] error responses do not always contain error.translation_key and related keys. #221

@EraYaN

Description

@EraYaN

Describe the bug
Websocket API error responses do not always contain error.translation_key and related keys.

To Reproduce

from homeassistant_api import WebsocketClient

with WebsocketClient(
    'wss://<domain>/api/websocket',
    '<long lived token>'
) as ws_client:
    res_id = ws_client.send("recorder/statistics_during_period",
        start_time="2025-12-30T17:20:00.000Z",
        end_time="2025-12-30T22:20:00.000Z",
        #statistic_ids=["sensor.display_switch_electric_consumption_kwh"],
        #period="hour" # this triggers the server side error because the fields are required.
    )
    resp = ws_client.recv(res_id)
    print(resp)

The server sends this back:

{'id': 2, 'type': 'result', 'success': False, 'error': {'code': 'invalid_format', 'message': "required key not provided @ data['period']. Got None\nrequired key not provided @ data['start_time']. Got None\nrequired key not provided @ data['statistic_ids']. Got None"}}

Resulting in the following output:

pydantic_core._pydantic_core.ValidationError: 3 validation errors for ErrorResponse
error.translation_key
  Field required [type=missing, input_value={'code': 'invalid_format'...tistic_ids']. Got None"}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.12/v/missing
error.translation_placeholders
  Field required [type=missing, input_value={'code': 'invalid_format'...tistic_ids']. Got None"}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.12/v/missing
error.translation_domain
  Field required [type=missing, input_value={'code': 'invalid_format'...tistic_ids']. Got None"}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.12/v/missing

Expected behavior
A clear and concise description of what you expected to happen.
The errors should be accepted without these keys since clearly they are optional for the server.

Desktop (please complete the following information):

  • OS: macOS
  • OS Version Sonoma 14.8.1
  • HomeAssistantAPI Version: 5.0.2.post2

Workaround

Patch the BaseError model to look like this:

class Error(BaseModel):
    code: str
    message: str
    translation_key: str | None = None
    translation_placeholders: dict[str, str] | None = None
    translation_domain: str | None = None

Proper solution will need some special pydantic magic I assume.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions