Skip to content

Commit 5bdb3dc

Browse files
committed
Reduce complexity of set_switch_state()
1 parent e2e7524 commit 5bdb3dc

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

plugwise/smile.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@
3737
from munch import Munch
3838

3939

40+
def model_to_switch_items(model: str, state: str, switch: Munch) -> tuple[str, Munch]:
41+
"""Translate state and switch attributes based on model name.
42+
43+
Helper function for set_switch_state().
44+
"""
45+
match model:
46+
case "dhw_cm_switch":
47+
switch.device = "toggle"
48+
switch.func_type = "toggle_functionality"
49+
switch.act_type = "domestic_hot_water_comfort_mode"
50+
case "cooling_ena_switch":
51+
switch.device = "toggle"
52+
switch.func_type = "toggle_functionality"
53+
switch.act_type = "cooling_enabled"
54+
case "lock":
55+
switch.func = "lock"
56+
state = "true" if state == STATE_ON else "false"
57+
58+
return (state, switch)
59+
60+
4061
class SmileAPI(SmileData):
4162
"""The Plugwise SmileAPI helper class for actual Plugwise devices."""
4263

@@ -381,20 +402,7 @@ async def set_switch_state(
381402
switch.device = "relay"
382403
switch.func_type = "relay_functionality"
383404
switch.func = "state"
384-
if model == "dhw_cm_switch":
385-
switch.device = "toggle"
386-
switch.func_type = "toggle_functionality"
387-
switch.act_type = "domestic_hot_water_comfort_mode"
388-
389-
if model == "cooling_ena_switch":
390-
switch.device = "toggle"
391-
switch.func_type = "toggle_functionality"
392-
switch.act_type = "cooling_enabled"
393-
394-
if model == "lock":
395-
switch.func = "lock"
396-
state = "true" if state == STATE_ON else "false"
397-
405+
(state, switch) = model_to_switch_items(model, state, switch)
398406
data = (
399407
f"<{switch.func_type}>"
400408
f"<{switch.func}>{state}</{switch.func}>"

0 commit comments

Comments
 (0)