Skip to content

Rework to simplify LED switches and new device_changed trigger - #88

Merged
sirkirby merged 11 commits into
mainfrom
dev
Jul 30, 2025
Merged

Rework to simplify LED switches and new device_changed trigger#88
sirkirby merged 11 commits into
mainfrom
dev

Conversation

@sirkirby

@sirkirby sirkirby commented Jul 30, 2025

Copy link
Copy Markdown
Owner

This pull request introduces several enhancements and fixes to the custom Home Assistant integration for managing UniFi Network policies and rules. The most significant changes include the addition of device LED state change detection and triggering, improved logging and dispatcher patterns, documentation updates, and minor corrections to coding standards and spelling errors.

fixes #84

New Features and Enhancements:

  • Device LED State Change Detection and Triggering:

    • Added methods to detect LED state changes and fire device triggers using Home Assistant's dispatcher pattern (fire_device_trigger_via_dispatcher and _check_for_device_state_changes in coordinator.py). [1] [2]
    • Introduced immediate trigger firing for LED toggles in the switch.py file to improve responsiveness (_async_toggle_rule method).
  • Improved Logging and Dispatcher Patterns:

    • Enhanced logging for LED-capable devices and dispatcher signal names to improve observability. [1] [2]
    • Added a new constant LOG_TRIGGERS to enable or disable logging for triggers. [1] [2]

Documentation Updates:

  • Project Overview and Structure:
    • Added a new .github/copilot-instructions.md file to provide a detailed overview of the project, including directory structure, libraries, and coding standards.

Bug Fixes and Code Quality Improvements:

  • Spelling Corrections:

    • Fixed multiple spelling errors in .cursor/rules/unr-rules.mdc (e.g., "fuctionality" → "functionality", "retrived" → "retrieved"). [1] [2]
  • Version Update:

    • Updated the integration version in manifest.json from 3.1.0 to 3.2.0.

sirkirby and others added 11 commits July 15, 2025 12:00
… handling in network.py to send only essential fields, preventing InvalidPayload errors on older UniFi Network versions.
Updated network API to send only essential fields, preventing InvalidPayload errors on older UniFi Network versions
…ity with older UniFi Network versions. Implemented a dual approach using DeviceSetLedStatus with a fallback to a minimal payload, enhancing error handling and logging for better debugging.
Refactor LED toggle functionality in network.py to use DeviceSetLedStatus with fallback
- Corrected typos in the coding standards section of the rules file.
- Added a new copilot instructions file outlining the project structure, libraries, and coding standards for the custom Home Assistant integration.
- Implemented a new method in `coordinator.py` to check for device state changes and fire corresponding triggers.
- Enhanced `fire_device_trigger_via_dispatcher` to dispatch device change events using Home Assistant's dispatcher pattern.
- Updated `switch.py` to fire immediate device triggers for LED toggles, improving responsiveness.
- Added support for device change triggers in `trigger.py`, allowing for manual firing and filtering based on device ID and change type.
- Improved logging for device trigger events to facilitate better monitoring and debugging.
- Introduced a new trigger for device changes to enhance the integration's capabilities.
- Updated the triggers.yaml file to include the 'device_changed' rule so HA properly registers the new trigger.
- now using aiounifi type Device for LED API integration and coordinator data
- removing device state management as that is no longer in scope of this integration
- other minor updates to comments and trigger functionality
@sirkirby sirkirby self-assigned this Jul 30, 2025
Copilot AI review requested due to automatic review settings July 30, 2025 20:22
@sirkirby sirkirby added bug Something isn't working enhancement New feature or request labels Jul 30, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request introduces device LED state change detection and triggering capabilities to the UniFi Network Rules integration, along with code quality improvements and documentation updates. The primary focus is on enabling Home Assistant automations to respond to LED state changes on UniFi devices.

  • Added device change detection with immediate and polling-based LED state monitoring
  • Implemented new device_changed trigger type with dispatcher-based event handling
  • Enhanced LED device management with proper Device object typing and state tracking

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
custom_components/unifi_network_rules/udm/network.py Refactored get_device_led_states to return typed Device objects instead of raw dictionaries
custom_components/unifi_network_rules/triggers.yaml Added device_changed trigger type configuration
custom_components/unifi_network_rules/trigger.py Implemented device trigger handling with dispatcher pattern and filtering
custom_components/unifi_network_rules/switch.py Added immediate trigger firing for LED toggles with optimistic state updates
custom_components/unifi_network_rules/manifest.json Updated version from 3.1.0 to 3.2.0
custom_components/unifi_network_rules/coordinator.py Added device state change detection and dispatcher-based trigger firing
.github/copilot-instructions.md Added project documentation with structure and coding standards
.cursor/rules/unr-rules.mdc Fixed spelling errors in coding practices documentation
Comments suppressed due to low confidence (2)

custom_components/unifi_network_rules/switch.py:594

  • The LED state values 'default' and 'off' should be defined as constants to avoid magic strings and improve maintainability.
                    return True

Comment thread custom_components/unifi_network_rules/udm/network.py
Comment thread custom_components/unifi_network_rules/trigger.py
Comment thread custom_components/unifi_network_rules/trigger.py
Comment thread custom_components/unifi_network_rules/coordinator.py
@sirkirby
sirkirby merged commit 82b213f into main Jul 30, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New UniFi devices not syncing

2 participants