Skip to content

Commit 00700a3

Browse files
julienldclaude
andcommitted
fix: use nested .get() calls to handle falsy values correctly
Addresses Gemini Code Assist review feedback: - Changed `or` fallbacks to nested `.get()` calls to avoid bugs with falsy values - Empty dict `{}` for attributes is valid and should not fall back - Empty string `''` for state is valid and should not fall back The nested `.get(key1, .get(key2, default))` pattern only falls back when the key is missing, not when the value is falsy, which is the correct behavior. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 65d5e93 commit 00700a3

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/ha_mcp/tools/tools_history.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,20 +328,20 @@ async def ha_get_history(
328328
# Get timestamps - WebSocket returns short-form (lc/lu) as Unix epoch floats
329329
# or long-form (last_changed/last_updated) as strings
330330
# Note: HA WebSocket API omits 'lc' when it equals 'lu' (optimization)
331-
last_updated_raw = state.get("lu") or state.get("last_updated")
332-
last_changed_raw = state.get("lc") or state.get("last_changed")
331+
last_updated_raw = state.get("lu", state.get("last_updated"))
332+
last_changed_raw = state.get("lc", state.get("last_changed"))
333333

334334
# If last_changed is missing, it means it equals last_updated
335335
if last_changed_raw is None and last_updated_raw is not None:
336336
last_changed_raw = last_updated_raw
337337

338338
state_entry = {
339-
"state": state.get("s") or state.get("state"),
339+
"state": state.get("s", state.get("state")),
340340
"last_changed": _convert_timestamp(last_changed_raw),
341341
"last_updated": _convert_timestamp(last_updated_raw),
342342
}
343343
if not minimal_response:
344-
state_entry["attributes"] = state.get("a") or state.get("attributes", {})
344+
state_entry["attributes"] = state.get("a", state.get("attributes", {}))
345345
formatted_states.append(state_entry)
346346

347347
entities_history.append(

0 commit comments

Comments
 (0)