Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 20 additions & 42 deletions homeassistant/components/miele/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,50 +98,28 @@ class MieleAppliance(IntEnum):
}


class StateStatus(IntEnum):
class StateStatus(MieleEnum, missing_to_none=True):
"""Define appliance states."""

RESERVED = 0
OFF = 1
ON = 2
PROGRAMMED = 3
WAITING_TO_START = 4
IN_USE = 5
PAUSE = 6
PROGRAM_ENDED = 7
FAILURE = 8
PROGRAM_INTERRUPTED = 9
IDLE = 10
RINSE_HOLD = 11
SERVICE = 12
SUPERFREEZING = 13
SUPERCOOLING = 14
SUPERHEATING = 15
SUPERCOOLING_SUPERFREEZING = 146
AUTOCLEANING = 147
NOT_CONNECTED = 255


STATE_STATUS_TAGS = {
StateStatus.OFF: "off",
StateStatus.ON: "on",
StateStatus.PROGRAMMED: "programmed",
StateStatus.WAITING_TO_START: "waiting_to_start",
StateStatus.IN_USE: "in_use",
StateStatus.PAUSE: "pause",
StateStatus.PROGRAM_ENDED: "program_ended",
StateStatus.FAILURE: "failure",
StateStatus.PROGRAM_INTERRUPTED: "program_interrupted",
StateStatus.IDLE: "idle",
StateStatus.RINSE_HOLD: "rinse_hold",
StateStatus.SERVICE: "service",
StateStatus.SUPERFREEZING: "superfreezing",
StateStatus.SUPERCOOLING: "supercooling",
StateStatus.SUPERHEATING: "superheating",
StateStatus.SUPERCOOLING_SUPERFREEZING: "supercooling_superfreezing",
StateStatus.AUTOCLEANING: "autocleaning",
StateStatus.NOT_CONNECTED: "not_connected",
}
reserved = 0
off = 1
on = 2
programmed = 3
waiting_to_start = 4
in_use = 5
pause = 6
program_ended = 7
failure = 8
program_interrupted = 9
idle = 10
rinse_hold = 11
service = 12
superfreezing = 13
supercooling = 14
superheating = 15
supercooling_superfreezing = 146
autocleaning = 147
not_connected = 255


class MieleActions(IntEnum):
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/miele/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ def available(self) -> bool:
return (
super().available
and self._device_id in self.coordinator.data.devices
and (self.device.state_status is not StateStatus.NOT_CONNECTED)
and (self.device.state_status is not StateStatus.not_connected)
)
47 changes: 27 additions & 20 deletions homeassistant/components/miele/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
DOMAIN,
PROGRAM_IDS,
PROGRAM_PHASE,
STATE_STATUS_TAGS,
MieleAppliance,
PlatePowerStep,
StateDryingStep,
Expand Down Expand Up @@ -195,7 +194,7 @@ class MieleSensorDefinition:
translation_key="status",
value_fn=lambda value: value.state_status,
device_class=SensorDeviceClass.ENUM,
options=sorted(set(STATE_STATUS_TAGS.values())),
options=sorted(set(StateStatus.keys())),
),
),
MieleSensorDefinition(
Expand Down Expand Up @@ -930,7 +929,7 @@ def __init__(
@property
def native_value(self) -> StateType:
"""Return the state of the sensor."""
return STATE_STATUS_TAGS.get(StateStatus(self.device.state_status))
return StateStatus(self.device.state_status).name

@property
def available(self) -> bool:
Expand Down Expand Up @@ -998,23 +997,27 @@ def _update_native_value(self) -> None:
"""Update the last value of the sensor."""

current_value = self.entity_description.value_fn(self.device)
current_status = StateStatus(self.device.state_status)
current_status = StateStatus(self.device.state_status).name

# report end-specific value when program ends (some devices are immediately reporting 0...)
if (
current_status == StateStatus.PROGRAM_ENDED
current_status == StateStatus.program_ended.name
and self.entity_description.end_value_fn is not None
):
self._attr_native_value = self.entity_description.end_value_fn(
self._attr_native_value
)

# keep value when program ends if no function is specified
elif current_status == StateStatus.PROGRAM_ENDED:
elif current_status == StateStatus.program_ended.name:
pass

# force unknown when appliance is not working (some devices are keeping last value until a new cycle starts)
elif current_status in (StateStatus.OFF, StateStatus.ON, StateStatus.IDLE):
elif current_status in (
StateStatus.off.name,
StateStatus.on.name,
StateStatus.idle.name,
):
self._attr_native_value = None

# otherwise, cache value and return it
Expand All @@ -1030,7 +1033,7 @@ class MieleAbsoluteTimeSensor(MieleRestorableSensor):
def _update_native_value(self) -> None:
"""Update the last value of the sensor."""
current_value = self.entity_description.value_fn(self.device)
current_status = StateStatus(self.device.state_status)
current_status = StateStatus(self.device.state_status).name

# The API reports with minute precision, to avoid changing
# the value too often, we keep the cached value if it differs
Expand All @@ -1043,11 +1046,15 @@ def _update_native_value(self) -> None:
< current_value
< self._previous_value + timedelta(seconds=90)
)
) or current_status == StateStatus.PROGRAM_ENDED:
) or current_status == StateStatus.program_ended.name:
return

# force unknown when appliance is not working (some devices are keeping last value until a new cycle starts)
if current_status in (StateStatus.OFF, StateStatus.ON, StateStatus.IDLE):
if current_status in (
StateStatus.off.name,
StateStatus.on.name,
StateStatus.idle.name,
):
self._attr_native_value = None

# otherwise, cache value and return it
Expand All @@ -1064,7 +1071,7 @@ class MieleConsumptionSensor(MieleRestorableSensor):
def _update_native_value(self) -> None:
"""Update the last value of the sensor."""
current_value = self.entity_description.value_fn(self.device)
current_status = StateStatus(self.device.state_status)
current_status = StateStatus(self.device.state_status).name
# Guard for corrupt restored value
restored_value = (
self._attr_native_value
Expand All @@ -1079,12 +1086,12 @@ def _update_native_value(self) -> None:

# Force unknown when appliance is not able to report consumption
if current_status in (
StateStatus.ON,
StateStatus.OFF,
StateStatus.PROGRAMMED,
StateStatus.WAITING_TO_START,
StateStatus.IDLE,
StateStatus.SERVICE,
StateStatus.on.name,
StateStatus.off.name,
StateStatus.programmed.name,
StateStatus.waiting_to_start.name,
StateStatus.idle.name,
StateStatus.service.name,
):
self._is_reporting = False
self._attr_native_value = None
Expand All @@ -1093,23 +1100,23 @@ def _update_native_value(self) -> None:
# only after a while, so it is necessary to force 0 until we see the 0 value coming from API, unless
# we already saw a valid value in this cycle from cache
elif (
current_status in (StateStatus.IN_USE, StateStatus.PAUSE)
current_status in (StateStatus.in_use.name, StateStatus.pause.name)
and not self._is_reporting
and last_value > 0
):
self._attr_native_value = current_value
self._is_reporting = True

elif (
current_status in (StateStatus.IN_USE, StateStatus.PAUSE)
current_status in (StateStatus.in_use.name, StateStatus.pause.name)
and not self._is_reporting
and current_value is not None
and cast(int, current_value) > 0
):
self._attr_native_value = 0

# keep value when program ends
elif current_status == StateStatus.PROGRAM_ENDED:
elif current_status == StateStatus.program_ended.name:
pass

else:
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/miele/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@
"program_ended": "Program ended",
"program_interrupted": "Program interrupted",
"programmed": "Programmed",
"reserved": "Reserved",
"rinse_hold": "Rinse hold",
"service": "Service",
"supercooling": "Supercooling",
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/miele/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MieleSwitchDefinition:
description=MieleSwitchDescription(
key="supercooling",
value_fn=lambda value: value.state_status,
on_value=StateStatus.SUPERCOOLING,
on_value=StateStatus.supercooling,
translation_key="supercooling",
on_cmd_data={PROCESS_ACTION: MieleActions.START_SUPERCOOL},
off_cmd_data={PROCESS_ACTION: MieleActions.STOP_SUPERCOOL},
Expand All @@ -73,7 +73,7 @@ class MieleSwitchDefinition:
description=MieleSwitchDescription(
key="superfreezing",
value_fn=lambda value: value.state_status,
on_value=StateStatus.SUPERFREEZING,
on_value=StateStatus.superfreezing,
translation_key="superfreezing",
on_cmd_data={PROCESS_ACTION: MieleActions.START_SUPERFREEZE},
off_cmd_data={PROCESS_ACTION: MieleActions.STOP_SUPERFREEZE},
Expand Down
Loading
Loading