From 86ab5f76e01336a91ae4602e2dee42b136b58099 Mon Sep 17 00:00:00 2001 From: mkmer Date: Mon, 9 Jan 2023 10:16:05 -0500 Subject: [PATCH] Whirlpool general code cleanup (#85387) --- homeassistant/components/whirlpool/sensor.py | 100 ++++++++++-------- .../components/whirlpool/strings.json | 44 ++++++++ .../components/whirlpool/translations/en.json | 44 ++++++++ tests/components/whirlpool/test_sensor.py | 22 ++-- 4 files changed, 152 insertions(+), 58 deletions(-) diff --git a/homeassistant/components/whirlpool/sensor.py b/homeassistant/components/whirlpool/sensor.py index 8a8df82eb55..ee2adb55a0f 100644 --- a/homeassistant/components/whirlpool/sensor.py +++ b/homeassistant/components/whirlpool/sensor.py @@ -15,7 +15,6 @@ from homeassistant.components.sensor import ( SensorEntityDescription, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import STATE_UNKNOWN from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -26,46 +25,46 @@ from . import WhirlpoolData from .const import DOMAIN TANK_FILL = { - "0": "Unknown", - "1": "Empty", + "0": "unknown", + "1": "empty", "2": "25%", "3": "50%", "4": "100%", - "5": "Active", + "5": "active", } MACHINE_STATE = { - MachineState.Standby: "Standby", - MachineState.Setting: "Setting", - MachineState.DelayCountdownMode: "Delay Countdown", - MachineState.DelayPause: "Delay Paused", - MachineState.SmartDelay: "Smart Delay", - MachineState.SmartGridPause: "Smart Grid Pause", - MachineState.Pause: "Pause", - MachineState.RunningMainCycle: "Running Maincycle", - MachineState.RunningPostCycle: "Running Postcycle", - MachineState.Exceptions: "Exception", - MachineState.Complete: "Complete", - MachineState.PowerFailure: "Power Failure", - MachineState.ServiceDiagnostic: "Service Diagnostic Mode", - MachineState.FactoryDiagnostic: "Factory Diagnostic Mode", - MachineState.LifeTest: "Life Test", - MachineState.CustomerFocusMode: "Customer Focus Mode", - MachineState.DemoMode: "Demo Mode", - MachineState.HardStopOrError: "Hard Stop or Error", - MachineState.SystemInit: "System Initialize", + MachineState.Standby: "standby", + MachineState.Setting: "setting", + MachineState.DelayCountdownMode: "delay_countdown", + MachineState.DelayPause: "delay_paused", + MachineState.SmartDelay: "smart_delay", + MachineState.SmartGridPause: "smart_grid_pause", + MachineState.Pause: "pause", + MachineState.RunningMainCycle: "running_maincycle", + MachineState.RunningPostCycle: "running_postcycle", + MachineState.Exceptions: "exception", + MachineState.Complete: "complete", + MachineState.PowerFailure: "power_failure", + MachineState.ServiceDiagnostic: "service_diagnostic_mode", + MachineState.FactoryDiagnostic: "factory_diagnostic_mode", + MachineState.LifeTest: "life_test", + MachineState.CustomerFocusMode: "customer_focus_mode", + MachineState.DemoMode: "demo_mode", + MachineState.HardStopOrError: "hard_stop_or_error", + MachineState.SystemInit: "system_initialize", } CYCLE_FUNC = [ - (WasherDryer.get_cycle_status_filling, "Cycle Filling"), - (WasherDryer.get_cycle_status_rinsing, "Cycle Rinsing"), - (WasherDryer.get_cycle_status_sensing, "Cycle Sensing"), - (WasherDryer.get_cycle_status_soaking, "Cycle Soaking"), - (WasherDryer.get_cycle_status_spinning, "Cycle Spinning"), - (WasherDryer.get_cycle_status_washing, "Cycle Washing"), + (WasherDryer.get_cycle_status_filling, "cycle_filling"), + (WasherDryer.get_cycle_status_rinsing, "cycle_rinsing"), + (WasherDryer.get_cycle_status_sensing, "cycle_sensing"), + (WasherDryer.get_cycle_status_soaking, "cycle_soaking"), + (WasherDryer.get_cycle_status_spinning, "cycle_spinning"), + (WasherDryer.get_cycle_status_washing, "cycle_washing"), ] - +DOOR_OPEN = "door_open" ICON_D = "mdi:tumble-dryer" ICON_W = "mdi:washing-machine" @@ -76,7 +75,7 @@ def washer_state(washer: WasherDryer) -> str | None: """Determine correct states for a washer.""" if washer.get_attribute("Cavity_OpStatusDoorOpen") == "1": - return "Door open" + return DOOR_OPEN machine_state = washer.get_machine_state() @@ -85,7 +84,7 @@ def washer_state(washer: WasherDryer) -> str | None: if func(washer): return cycle_name - return MACHINE_STATE.get(machine_state, STATE_UNKNOWN) + return MACHINE_STATE.get(machine_state, None) @dataclass @@ -106,15 +105,21 @@ SENSORS: tuple[WhirlpoolSensorEntityDescription, ...] = ( WhirlpoolSensorEntityDescription( key="state", name="State", - icon=ICON_W, - has_entity_name=True, + translation_key="whirlpool_machine", + device_class=SensorDeviceClass.ENUM, + options=( + list(MACHINE_STATE.values()) + + [value for _, value in CYCLE_FUNC] + + [DOOR_OPEN] + ), value_fn=washer_state, ), WhirlpoolSensorEntityDescription( key="DispenseLevel", name="Detergent Level", - icon=ICON_W, - has_entity_name=True, + translation_key="whirlpool_tank", + device_class=SensorDeviceClass.ENUM, + options=list(TANK_FILL.values()), value_fn=lambda WasherDryer: TANK_FILL[ WasherDryer.get_attribute("WashCavity_OpStatusBulkDispense1Level") ], @@ -126,8 +131,6 @@ SENSOR_TIMER: tuple[SensorEntityDescription] = ( key="timeremaining", name="End Time", device_class=SensorDeviceClass.TIMESTAMP, - icon=ICON_W, - has_entity_name=True, ), ) @@ -186,19 +189,21 @@ class WasherDryerClass(SensorEntity): washdry: WasherDryer, ) -> None: """Initialize the washer sensor.""" - self._name = name.capitalize() self._wd: WasherDryer = washdry - if self._name == "Dryer": + if name == "dryer": self._attr_icon = ICON_D + else: + self._attr_icon = ICON_W self.entity_description: WhirlpoolSensorEntityDescription = description - self._attr_unique_id = f"{said}-{description.key}" self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, said)}, - name=self._name, + name=name.capitalize(), manufacturer="Whirlpool", ) + self._attr_has_entity_name = True + self._attr_unique_id = f"{said}-{description.key}" async def async_added_to_hass(self) -> None: """Connect washer/dryer to the cloud.""" @@ -232,21 +237,22 @@ class WasherDryerTimeClass(RestoreSensor): washdry: WasherDryer, ) -> None: """Initialize the washer sensor.""" - self._name = name.capitalize() self._wd: WasherDryer = washdry - if self._name == "Dryer": + if name == "dryer": self._attr_icon = ICON_D + else: + self._attr_icon = ICON_W self.entity_description: SensorEntityDescription = description - self._attr_unique_id = f"{said}-{description.key}" self._running: bool | None = None - self._timestamp: datetime | None = None self._attr_device_info = DeviceInfo( identifiers={(DOMAIN, said)}, - name=self._name, + name=name.capitalize(), manufacturer="Whirlpool", ) + self._attr_has_entity_name = True + self._attr_unique_id = f"{said}-{description.key}" async def async_added_to_hass(self) -> None: """Connect washer/dryer to the cloud.""" diff --git a/homeassistant/components/whirlpool/strings.json b/homeassistant/components/whirlpool/strings.json index f6da89f1974..f0b4f03da8a 100644 --- a/homeassistant/components/whirlpool/strings.json +++ b/homeassistant/components/whirlpool/strings.json @@ -14,5 +14,49 @@ "unknown": "[%key:common::config_flow::error::unknown%]", "no_appliances": "No supported appliances found" } + }, + "entity": { + "sensor": { + "whirlpool_machine": { + "state": { + "standby": "[%key:common::state::standby%]", + "setting": "Setting", + "delay_countdown": "Delay Countdown", + "delay_paused": "Delay Paused", + "smart_delay": "Smart Delay", + "smart_grid_pause": "Smart Delay", + "pause": "[%key:common::state::paused%]", + "running_maincycle": "Running Maincycle", + "running_postcycle": "Running Postcycle", + "exception": "Exception", + "complete": "Complete", + "power_failure": "Power Failure", + "service_diagnostic_mode": "Service Diagnostic Mode", + "factory_diagnostic_mode": "Factory Diagnostic Mode", + "life_test": "Life Test", + "customer_focus_mode": "Customer Focus Mode", + "demo_mode": "Demo Mode", + "hard_stop_or_error": "Hard Stop or Error", + "system_initialize": "System Initialize", + "cycle_filling": "Cycle Filling", + "cycle_rinsing": "Cycle Rinsing", + "cycle_sensing": "Cycle Sensing", + "cycle_soaking": "Cycle Soaking", + "cycle_spinning": "Cycle Spinning", + "cycle_washing": "Cycle Washing", + "door_open": "Door Open" + } + }, + "whirlpool_tank": { + "state": { + "unknown": "Unknown", + "empty": "Empty", + "25%": "25%", + "50%": "50%", + "100%": "100%", + "active": "[%key:common::state::active%]" + } + } + } } } diff --git a/homeassistant/components/whirlpool/translations/en.json b/homeassistant/components/whirlpool/translations/en.json index 99f65933631..15343baf1c2 100644 --- a/homeassistant/components/whirlpool/translations/en.json +++ b/homeassistant/components/whirlpool/translations/en.json @@ -14,5 +14,49 @@ } } } + }, + "entity": { + "sensor": { + "whirlpool_machine": { + "state": { + "complete": "Complete", + "customer_focus_mode": "Customer Focus Mode", + "cycle_filling": "Cycle Filling", + "cycle_rinsing": "Cycle Rinsing", + "cycle_sensing": "Cycle Sensing", + "cycle_soaking": "Cycle Soaking", + "cycle_spinning": "Cycle Spinning", + "cycle_washing": "Cycle Washing", + "delay_countdown": "Delay Countdown", + "delay_paused": "Delay Paused", + "demo_mode": "Demo Mode", + "door_open": "Door Open", + "exception": "Exception", + "factory_diagnostic_mode": "Factory Diagnostic Mode", + "hard_stop_or_error": "Hard Stop or Error", + "life_test": "Life Test", + "pause": "Pause", + "power_failure": "Power Failure", + "running_maincycle": "Running Maincycle", + "running_postcycle": "Running Postcycle", + "service_diagnostic_mode": "Service Diagnostic Mode", + "setting": "Setting", + "smart_delay": "Smart Delay", + "smart_grid_pause": "Smart Delay", + "standby": "Standby", + "system_initialize": "System Initialize" + } + }, + "whirlpool_tank": { + "state": { + "100%": "100%", + "25%": "25%", + "50%": "50%", + "active": "Active", + "empty": "Empty", + "unknown": "Unknown" + } + } + } } } \ No newline at end of file diff --git a/tests/components/whirlpool/test_sensor.py b/tests/components/whirlpool/test_sensor.py index 658613b48c1..2452c9084b6 100644 --- a/tests/components/whirlpool/test_sensor.py +++ b/tests/components/whirlpool/test_sensor.py @@ -74,7 +74,7 @@ async def test_dryer_sensor_values( assert entry state = hass.states.get(entity_id) assert state is not None - assert state.state == "Standby" + assert state.state == "standby" state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None @@ -95,13 +95,13 @@ async def test_dryer_sensor_values( state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Running Maincycle" + assert state.state == "running_maincycle" mock_instance.get_machine_state.return_value = MachineState.Complete state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Complete" + assert state.state == "complete" async def test_washer_sensor_values( @@ -138,7 +138,7 @@ async def test_washer_sensor_values( assert entry state = hass.states.get(entity_id) assert state is not None - assert state.state == "Standby" + assert state.state == "standby" state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None @@ -165,7 +165,7 @@ async def test_washer_sensor_values( state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Cycle Filling" + assert state.state == "cycle_filling" mock_instance.get_cycle_status_filling.return_value = False mock_instance.get_cycle_status_rinsing.return_value = True @@ -180,7 +180,7 @@ async def test_washer_sensor_values( state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Cycle Rinsing" + assert state.state == "cycle_rinsing" mock_instance.get_cycle_status_rinsing.return_value = False mock_instance.get_cycle_status_sensing.return_value = True @@ -195,7 +195,7 @@ async def test_washer_sensor_values( state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Cycle Sensing" + assert state.state == "cycle_sensing" mock_instance.get_cycle_status_sensing.return_value = False mock_instance.get_cycle_status_soaking.return_value = True @@ -210,7 +210,7 @@ async def test_washer_sensor_values( state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Cycle Soaking" + assert state.state == "cycle_soaking" mock_instance.get_cycle_status_soaking.return_value = False mock_instance.get_cycle_status_spinning.return_value = True @@ -225,7 +225,7 @@ async def test_washer_sensor_values( state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Cycle Spinning" + assert state.state == "cycle_spinning" mock_instance.get_cycle_status_spinning.return_value = False mock_instance.get_cycle_status_washing.return_value = True @@ -240,14 +240,14 @@ async def test_washer_sensor_values( state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Cycle Washing" + assert state.state == "cycle_washing" mock_instance.get_machine_state.return_value = MachineState.Complete mock_instance.attr_value_to_bool.side_effect = None mock_instance.get_attribute.side_effect = side_effect_function_open_door state = await update_sensor_state(hass, entity_id, mock_instance) assert state is not None - assert state.state == "Door open" + assert state.state == "door_open" async def test_restore_state(