Skip to content

Use call_service with return_response for response-returning HA services - #1470

Open
FrankBakkerNl with Copilot wants to merge 15 commits into
mainfrom
copilot/fix-callservicewithresponseasync
Open

FrankBakkerNl with Copilot wants to merge 15 commits into
mainfrom
copilot/fix-callservicewithresponseasync

Conversation

Copilot AI commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

CallServiceWithResponseAsync was always routing through Home Assistant's execute_script websocket command, which requires admin privileges even when the underlying service does not. This change switches response-returning service calls to the native call_service command with return_response: true, so non-admin tokens can use services such as todo.get_items and calendar.get_events when otherwise authorized.

  • Protocol change

    • Add return_response to the internal CallServiceCommand
    • Update CallServiceWithResponseAsync to send a plain call_service command instead of wrapping the call in execute_script
  • Behavior preservation

    • Keep return_response opt-in and omit it from normal call_service payloads when unset
    • Leave existing non-response service-call behavior unchanged
  • Test coverage

    • Add focused unit coverage for the response-returning command path, including the default CancellationToken.None branch
    • Update websocket integration coverage to validate end-to-end response payload handling for calendar.get_events
    • Make the integration Home Assistant mock aware of return_response requests
await connection.CallServiceWithResponseAsync(
    "calendar",
    "get_events",
    serviceData,
    target,
    cancellationToken);

This now emits a websocket call_service command equivalent to:

{
  "type": "call_service",
  "domain": "calendar",
  "service": "get_events",
  "service_data": { ... },
  "target": { ... },
  "return_response": true
}

Copilot AI and others added 4 commits September 25, 2026 11:33
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CallServiceWithResponseAsync to use call_service command Use call_service with return_response for response-returning HA services Sep 25, 2026
Copilot AI requested a review from FrankBakkerNl September 25, 2026 11:39
Copilot AI and others added 8 commits September 25, 2026 12:27
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>

@helto4real helto4real left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes for one reproducible test failure on commit 8a2604066a9621e694eb598b5f619b8be47617ee.

[P2] Update the existing HassModel regression test for the new service command

CallServiceWithResponseAsync now sends CallServiceCommand, but AppScopedHaContextProviderTest.TestCallServiceWithResponseAsync still looks for a CallExecuteScriptCommand in the mock invocations. Its Single(...) call at src/HassModel/NetDaemon.HassModel.Tests/Internal/AppScopedHaContextProviderTest.cs:144 therefore throws InvalidOperationException: Sequence contains no matching element.

This reproduces locally and matches the failure in CI run 36135897430. Please update the test to assert CallServiceCommand, ReturnResponse == true, and correct forwarding of domain, service, service data, and target, retaining coverage of the IHaContext forwarding path. Then rerun the required CI checks.

Validation on the reviewed commit:

  • Client tests: 172 passed.
  • HassModel tests: 154 passed, 1 failed as described above.
  • Selected calendar integration and helper tests: 7 passed, including execution against an isolated Home Assistant 2026.8.2 container.

I found no other blocking production-code issues in this review. The label check is also failing and needs to be addressed separately. Validation did not include an actual non-admin token, Home Assistant beta, or the full integration suite.

Co-authored-by: FrankBakkerNl <13922018+FrankBakkerNl@users.noreply.github.com>
@FrankBakkerNl
FrankBakkerNl marked this pull request as ready for review September 30, 2026 12:58
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83%. Comparing base (250a6f8) to head (03fcdbe).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@         Coverage Diff          @@
##           main   #1470   +/-   ##
====================================
- Coverage    83%     83%   -1%     
====================================
  Files       201     201           
  Lines      4173    4164    -9     
  Branches    477     477           
====================================
- Hits       3480    3466   -14     
- Misses      496     501    +5     
  Partials    197     197           
Flag Coverage Δ
unittests 83% <100%> (-1%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CallServiceWithResponseAsync always wraps the call in execute_script, requiring admin

3 participants