Skip to content

Comments

Add export_config service#376

Merged
Hankanman merged 1 commit intomainfrom
feature/export-config-service
Feb 22, 2026
Merged

Add export_config service#376
Hankanman merged 1 commit intomainfrom
feature/export-config-service

Conversation

@Hankanman
Copy link
Owner

@Hankanman Hankanman commented Feb 22, 2026

Summary

  • Adds a new area_occupancy.export_config HA service that returns the complete integration configuration (merged config_entry.data and config_entry.options) as a service response
  • Area configs are reordered so area_id appears as the first key for readability
  • Follows the same registration pattern as the existing run_analysis service

Test plan

  • Run area_occupancy.export_config from Developer Tools > Services and verify output contains the full config
  • Verify area_id appears first in each area block
  • Confirm existing run_analysis service still works

Closes #287

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added a new "Export Config" service that exports the complete integration configuration as YAML format.

Adds a new HA service that returns the complete integration configuration
(merged config_entry.data and options) directly as a service response,
rendered as YAML in Developer Tools. Area configs are reordered so
area_id appears first for readability.

Closes #287

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 22, 2026

Walkthrough

The PR introduces a new "export_config" service that allows exporting the complete integration configuration in YAML format. The implementation includes a service handler in the service module and a corresponding service declaration in the services manifest.

Changes

Cohort / File(s) Summary
Export Config Service
custom_components/area_occupancy/service.py, custom_components/area_occupancy/services.yaml
Added new export_config service with internal handler functions (_export_config, handle_export_config) that assembles and exports the complete integration configuration as YAML, including service registration and manifest declaration.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add export_config service' accurately and concisely describes the main change: adding a new service named export_config to the integration.
Linked Issues check ✅ Passed This PR adds infrastructure (export_config service) enabling YAML configuration support by allowing users to export the complete configuration, which is a necessary prerequisite step toward the YAML configuration feature requested in issue #287.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the export_config service: service logic in service.py, service registration, and service declaration in services.yaml. No unrelated changes detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/export-config-service

Comment @coderabbitai help to get the list of available commands and usage tips.

@Hankanman Hankanman self-assigned this Feb 22, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
custom_components/area_occupancy/service.py (2)

187-188: Consider expanding the docstring to include Args and Returns.

Per coding guidelines, function docstrings should document parameters and return values.

📝 Proposed docstring expansion
 async def _export_config(hass: HomeAssistant, call: ServiceCall) -> dict[str, Any]:
-    """Export the complete integration configuration as YAML."""
+    """Export the complete integration configuration as YAML.
+
+    Args:
+        hass: Home Assistant instance
+        call: Service call data (unused, required by service handler pattern)
+
+    Returns:
+        Dictionary containing merged config_entry.data and config_entry.options
+
+    Raises:
+        HomeAssistantError: If coordinator not found or config export fails
+    """
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@custom_components/area_occupancy/service.py` around lines 187 - 188, The
docstring for async function _export_config(hass: HomeAssistant, call:
ServiceCall) is minimal; update it to include an Args section listing hass
(HomeAssistant) and call (ServiceCall) with brief descriptions, and a Returns
section describing the returned dict[str, Any] (exported YAML/config structure
or error shape), keeping the one-line summary intact and following the project's
docstring style/convention.

204-207: Use specific exceptions and include stack traces in error logging.

Per coding guidelines, catch specific exceptions rather than generic Exception, and include exc_info=True for stack traces. The get_coordinator helper already raises HomeAssistantError, and dict operations can raise KeyError/TypeError.

♻️ Proposed improvement for exception handling
-    except Exception as err:
-        error_msg = f"Failed to export config: {err}"
-        _LOGGER.error(error_msg)
+    except (HomeAssistantError, KeyError, TypeError) as err:
+        error_msg = f"Failed to export config: {err}"
+        _LOGGER.error(error_msg, exc_info=True)
         raise HomeAssistantError(error_msg) from err

As per coding guidelines: "Use specific exceptions over generic Exception and include stack traces in logging."

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@custom_components/area_occupancy/service.py` around lines 204 - 207, The
catch-all except block should be replaced with specific exception handling:
catch HomeAssistantError separately (re-raise it unchanged), and catch likely
runtime exceptions such as KeyError and TypeError from dict ops and
serialization, log them with the stack trace by calling _LOGGER.error(...) with
exc_info=True and a clear message (use the same error_msg pattern), then raise a
new HomeAssistantError(error_msg) from the original exception; ensure references
to get_coordinator, error_msg, _LOGGER.error, and HomeAssistantError remain and
are used when locating and implementing the changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@custom_components/area_occupancy/service.py`:
- Around line 187-188: The docstring for async function _export_config(hass:
HomeAssistant, call: ServiceCall) is minimal; update it to include an Args
section listing hass (HomeAssistant) and call (ServiceCall) with brief
descriptions, and a Returns section describing the returned dict[str, Any]
(exported YAML/config structure or error shape), keeping the one-line summary
intact and following the project's docstring style/convention.
- Around line 204-207: The catch-all except block should be replaced with
specific exception handling: catch HomeAssistantError separately (re-raise it
unchanged), and catch likely runtime exceptions such as KeyError and TypeError
from dict ops and serialization, log them with the stack trace by calling
_LOGGER.error(...) with exc_info=True and a clear message (use the same
error_msg pattern), then raise a new HomeAssistantError(error_msg) from the
original exception; ensure references to get_coordinator, error_msg,
_LOGGER.error, and HomeAssistantError remain and are used when locating and
implementing the changes.

@Hankanman Hankanman merged commit c4ce608 into main Feb 22, 2026
5 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.

Add yaml configuration options

1 participant