Silence ERROR log on override_properties recovery path#491
Open
pszypowicz wants to merge 1 commit into
Open
Conversation
override_properties is designed to recover gracefully when the characteristic's existing value falls outside a newly-restricted valid_values set (e.g. SecuritySystemCurrentState defaults to StayArmed=0, but a consumer configures the characteristic for an alarm panel that does not advertise ARM_HOME and so removes 0 from valid_values). The recovery is silent from the caller's perspective: ValueError is caught and self.value is reset via _get_default_value(). The validator it relied on, valid_value_or_raise, still calls logger.error() before raising, leaking an "is an invalid value" line into the application log even though the situation is expected and recovered. Downstream this manifests in Home Assistant as spurious pyhap.characteristic ERRORs at every HomeKit accessory build for alarm panels that restrict StayArmed (issue ikalchev#473 and home-assistant/core#130564, #156142). Replace the try/except-around-validator pattern in override_properties with an explicit silent membership check. valid_value_or_raise itself is unchanged so callers like set_value (where ERROR-level logging is appropriate) keep their behavior.
pszypowicz
added a commit
to pszypowicz/ha-pyhap-override-quiet
that referenced
this pull request
May 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #473
Summary
Service.configure_charcallsCharacteristic.override_propertiesto install a restrictedvalid_valuesset, thenset_valueto seed an initial value.override_propertiesis designed to recover gracefully when the characteristic's existing_value(the format default, set in__init__) falls outside the newly-restrictedvalid_values- it catches theValueErrorand falls back via_get_default_value().The validator it uses,
valid_value_or_raise, callslogger.error()before raising. So even though the recovery path is the intended behavior, every consumer that restrictsvalid_valuesfor a characteristic whose format-default is excluded leaks a spurious ERROR log line.This is what users hit at every HomeKit accessory build for alarm panels that do not advertise
ARM_HOME: theSecuritySystemCurrentStateandSecuritySystemTargetStatedefault toStayArmed=0, but the configuredvalid_valuesexcludes 0. The existing recovery still kicks in correctly (HA's HomeKit accessory works fine), but the log fills with red entries that look like real failures.Root cause
valid_value_or_raiseis appropriate when the caller wants the validation error surfaced (e.g.set_value), but insideoverride_propertiesthe exception is by design caught and ignored - so the ERROR log is misleading.Fix
Replace the try/except-around-validator pattern with an explicit silent membership check.
valid_value_or_raiseitself is left unchanged so other callers retain ERROR-level logging.Behavior matrix is unchanged:
to_valid_valuevalid_valuesVerification
pytest tests/passes the relevant suite (one unrelated pre-existing failure on Python 3.14 intest_accessory_driver.py::test_start_from_sync, from upstream's use of the removedasyncio.SafeChildWatcher).characteristic.pyagainst a live Home Assistant install with a HomeKit-bridged alarm panel: the twopyhap.characteristicERROR lines that appeared on every restart are gone, and the accessory still works (state changes propagate, target-state changes from the Home app reach the panel).Related
Happy to add a CHANGELOG entry if you'd like - I kept the diff to the fix itself.