refactor: simplify washer toggle switches, move validate_fn to coordinator
/simplify pass on the bubble soak/pre-wash/intensive switches: - Move validate_fn dispatch from switch.py into coordinator.async_send_command, next to the existing write_fn getattr -- every platform gets validation for free instead of switch.py hand-rolling it alone, and it avoids building the full resources snapshot twice per write (switch.py was calling coordinator.last_resources twice; the coordinator now snapshots once, and only when a validate_fn is actually present). - Collapse the four per-switch factories (write/value/exists/validate) plus the _AVAILABILITY_FIELD side-table into one _bool_option_switch() that builds the SwitchDesc directly, so the three call sites read as one line each instead of six, and a typo'd prefix can no longer silently KeyError against a separate lookup table. - Rename _dosing_alarm_exists to _option_exists and reuse it for the new switches too -- it was already the exact same "is this token present" check the toggles need. No behavior change; re-verified write_fn/rep_fn/exists_fn/validate_fn against the same fixtures and golden state-key sets as before.
This commit is contained in:
3 files changed
+42
-65
No files matched your search
@@ -12,6 +12,7 @@ import cbor2
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import issue_registry as ir
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
@@ -509,13 +510,23 @@ class LocalThingsCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
||||
|
||||
async def async_send_command(self, bound_entity: BoundEntity,
|
||||
payload: Any) -> None:
|
||||
"""Write a value to the device. Fire-and-forget style."""
|
||||
"""Write a value to the device. Fire-and-forget style.
|
||||
|
||||
A description-level validate_fn (currently SwitchDesc only) runs
|
||||
here rather than per-platform, so rejecting a write with a
|
||||
user-facing message -- as opposed to write_fn's silent no-op below
|
||||
-- is available to every platform for free."""
|
||||
desc = bound_entity.desc
|
||||
write_fn = getattr(desc, 'write_fn', None)
|
||||
if write_fn is None:
|
||||
return
|
||||
href = bound_entity.href
|
||||
rep = self._cache.get(href or '') or {}
|
||||
validate_fn = getattr(desc, 'validate_fn', None)
|
||||
if validate_fn is not None:
|
||||
error = validate_fn(payload, rep, self._cache.snapshot())
|
||||
if error:
|
||||
raise ServiceValidationError(error)
|
||||
result = write_fn(payload, rep, href)
|
||||
if result is None:
|
||||
self._log.warning("write_fn rejected payload %r for %s", payload, href)
|
||||
|
||||
@@ -233,7 +233,7 @@ def _dosing_low(prefix):
|
||||
rep.get('x.com.samsung.da.options'), prefix) not in (None, 'Off')
|
||||
|
||||
|
||||
def _dosing_alarm_exists(prefix):
|
||||
def _option_exists(prefix):
|
||||
return lambda rep, resources: option_value(
|
||||
rep.get('x.com.samsung.da.options'), prefix) is not None
|
||||
|
||||
@@ -254,10 +254,11 @@ def _dosing_alarm_exists(prefix):
|
||||
# control out there. 'F0'/'00' is treated as available/unavailable on that
|
||||
# evidence. exists_fn (device-level presence) still only runs once, against
|
||||
# the setup-time snapshot, so it isn't a fit for this per-course check --
|
||||
# validate_fn runs on every write attempt instead, rejecting an on-write for
|
||||
# a course whose byte isn't 'F0' with a user-facing error rather than
|
||||
# validate_fn runs on every write attempt instead (dispatched from
|
||||
# coordinator.async_send_command, ahead of write_fn), rejecting an on-write
|
||||
# for a course whose byte isn't 'F0' with a user-facing error rather than
|
||||
# silently no-opping against the device.
|
||||
def _bool_option_write(prefix):
|
||||
def _bool_option_switch(key, name, icon, prefix, availability_field):
|
||||
def write(p, rep, href=None):
|
||||
if p not in ('On', 'Off'):
|
||||
return None
|
||||
@@ -267,34 +268,14 @@ def _bool_option_write(prefix):
|
||||
return ['course', 'vs', '0'], {
|
||||
'x.com.samsung.da.options': replace_in_options(opts, prefix, p),
|
||||
}
|
||||
return write
|
||||
|
||||
|
||||
def _bool_option_value(prefix):
|
||||
return lambda rep: option_value(rep.get('x.com.samsung.da.options'), prefix) == 'On'
|
||||
|
||||
|
||||
def _bool_option_exists(prefix):
|
||||
return lambda rep, resources: option_value(
|
||||
rep.get('x.com.samsung.da.options'), prefix) is not None
|
||||
|
||||
|
||||
_AVAILABILITY_FIELD = {
|
||||
'BubbleSoak': 'BubbleSoakSet',
|
||||
'PreWashSetting': 'PreWashAvailableSet',
|
||||
'IntensiveSetting': 'IntensiveAvailableSet',
|
||||
}
|
||||
|
||||
|
||||
def _bool_option_validate(prefix, human_name):
|
||||
"""Reject turning `prefix` on when the selected course's byte in its
|
||||
availability bitmap isn't 'F0'. Turning off is never blocked. Falls back
|
||||
to allowing the write whenever the availability data can't be resolved
|
||||
(unrecognized course, missing/mismatched-length bitmap) rather than
|
||||
guessing -- a false rejection is worse than an occasional no-op write."""
|
||||
availability_field = _AVAILABILITY_FIELD[prefix]
|
||||
|
||||
def validate(p, rep, resources):
|
||||
"""Reject turning on when the selected course's byte in
|
||||
`availability_field` isn't 'F0'. Turning off is never blocked. Falls
|
||||
back to allowing the write whenever the availability data can't be
|
||||
resolved (unrecognized course, missing/mismatched-length bitmap)
|
||||
rather than guessing -- a false rejection is worse than an
|
||||
occasional no-op write."""
|
||||
if p != 'On':
|
||||
return None
|
||||
opts = rep.get('x.com.samsung.da.options') or []
|
||||
@@ -309,9 +290,16 @@ def _bool_option_validate(prefix, human_name):
|
||||
if len(pairs) != len(courses):
|
||||
return None
|
||||
if pairs[courses.index(current)] != 'F0':
|
||||
return f"{human_name} isn't available on the selected cycle."
|
||||
return f"{name} isn't available on the selected cycle."
|
||||
return None
|
||||
return validate
|
||||
|
||||
return SwitchDesc(
|
||||
key=key, name=name, icon=icon, entity_category='config',
|
||||
exists_fn=_option_exists(prefix),
|
||||
rep_fn=lambda rep: option_value(rep.get('x.com.samsung.da.options'), prefix) == 'On',
|
||||
write_fn=write,
|
||||
validate_fn=validate,
|
||||
)
|
||||
|
||||
|
||||
WASHER_COURSE = Capability(
|
||||
@@ -364,29 +352,17 @@ WASHER_COURSE = Capability(
|
||||
write_fn=_level_write('SoftenerLevel2Ctrl')),
|
||||
BinarySensorDesc(key='detergent_low', name='Detergent low',
|
||||
icon='mdi:alert-circle-outline', device_class='problem',
|
||||
exists_fn=_dosing_alarm_exists('DetergentAlarm'),
|
||||
exists_fn=_option_exists('DetergentAlarm'),
|
||||
rep_fn=_dosing_low('DetergentAlarm')),
|
||||
BinarySensorDesc(key='softener_low', name='Softener low',
|
||||
icon='mdi:alert-circle-outline', device_class='problem',
|
||||
exists_fn=_dosing_alarm_exists('SoftenerAlarm'),
|
||||
exists_fn=_option_exists('SoftenerAlarm'),
|
||||
rep_fn=_dosing_low('SoftenerAlarm')),
|
||||
SwitchDesc(key='bubble_soak', name='Bubble soak', icon='mdi:chart-bubble',
|
||||
entity_category='config',
|
||||
exists_fn=_bool_option_exists('BubbleSoak'),
|
||||
rep_fn=_bool_option_value('BubbleSoak'),
|
||||
write_fn=_bool_option_write('BubbleSoak'),
|
||||
validate_fn=_bool_option_validate('BubbleSoak', 'Bubble soak')),
|
||||
SwitchDesc(key='pre_wash', name='Pre wash', icon='mdi:washing-machine',
|
||||
entity_category='config',
|
||||
exists_fn=_bool_option_exists('PreWashSetting'),
|
||||
rep_fn=_bool_option_value('PreWashSetting'),
|
||||
write_fn=_bool_option_write('PreWashSetting'),
|
||||
validate_fn=_bool_option_validate('PreWashSetting', 'Pre wash')),
|
||||
SwitchDesc(key='intensive', name='Intensive', icon='mdi:washing-machine',
|
||||
entity_category='config',
|
||||
exists_fn=_bool_option_exists('IntensiveSetting'),
|
||||
rep_fn=_bool_option_value('IntensiveSetting'),
|
||||
write_fn=_bool_option_write('IntensiveSetting'),
|
||||
validate_fn=_bool_option_validate('IntensiveSetting', 'Intensive')),
|
||||
_bool_option_switch('bubble_soak', 'Bubble soak', 'mdi:chart-bubble',
|
||||
'BubbleSoak', 'BubbleSoakSet'),
|
||||
_bool_option_switch('pre_wash', 'Pre wash', 'mdi:washing-machine',
|
||||
'PreWashSetting', 'PreWashAvailableSet'),
|
||||
_bool_option_switch('intensive', 'Intensive', 'mdi:washing-machine',
|
||||
'IntensiveSetting', 'IntensiveAvailableSet'),
|
||||
),
|
||||
)
|
||||
@@ -4,7 +4,6 @@ from __future__ import annotations
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .registry.entities import SwitchDesc
|
||||
@@ -39,16 +38,7 @@ class LocalThingsSwitch(LocalThingsEntity, SwitchEntity):
|
||||
return (self.coordinator.data or {}).get(self._state_key)
|
||||
|
||||
async def async_turn_on(self, **kwargs) -> None:
|
||||
await self._send('On')
|
||||
await self.coordinator.async_send_command(self._bound, 'On')
|
||||
|
||||
async def async_turn_off(self, **kwargs) -> None:
|
||||
await self._send('Off')
|
||||
|
||||
async def _send(self, payload: str) -> None:
|
||||
desc: SwitchDesc = self._bound.desc
|
||||
if desc.validate_fn is not None:
|
||||
rep = self.coordinator.last_resources.get(self._bound.href) or {}
|
||||
error = desc.validate_fn(payload, rep, self.coordinator.last_resources)
|
||||
if error:
|
||||
raise ServiceValidationError(error)
|
||||
await self.coordinator.async_send_command(self._bound, payload)
|
||||
await self.coordinator.async_send_command(self._bound, 'Off')
|
||||
Reference in new issue
Block a user