Skip to content

Commit

Permalink
Tumble dryer cleanup, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ofalvai committed Oct 13, 2021
1 parent 39474aa commit 1891ed2
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 29 deletions.
47 changes: 23 additions & 24 deletions custom_components/candy/client/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,6 @@ def __str__(self):
return "%s" % self


class DryerProgramState(Enum):
STOPPED = 0
RUNNING = 2
END = 3

def __str__(self):
if self == DryerProgramState.STOPPED:
return "Stopped"
elif self == DryerProgramState.RUNNING:
return "Running"
elif self == DryerProgramState.END:
return "End"
else:
return "%s" % self


@dataclass
class WashingMachineStatus:
machine_state: MachineState
Expand All @@ -113,6 +97,22 @@ def from_json(cls, json):
)


class DryerProgramState(Enum):
STOPPED = 0
RUNNING = 2
END = 3

def __str__(self):
if self == DryerProgramState.STOPPED:
return "Stopped"
elif self == DryerProgramState.RUNNING:
return "Running"
elif self == DryerProgramState.END:
return "End"
else:
return "%s" % self


@dataclass
class TumbleDryerStatus:
machine_state: MachineState
Expand All @@ -121,27 +121,26 @@ class TumbleDryerStatus:
remaining_minutes: int
remote_control: bool
dry_level: int
dry_level_selected: int
refresh: bool
need_clean_filter: bool
full_water_tank: bool
drylevel_selected: int
door_close: bool
water_tank_full: bool
door_closed: bool

@classmethod
def from_json(cls, json):
return cls(
machine_state=MachineState(int(json["StatoTD"])), # TODO?
machine_state=MachineState(int(json["StatoTD"])),
program_state=DryerProgramState(int(json["PrPh"])),
program=int(json["Pr"]),
remaining_minutes=int(json["RemTime"]),
remote_control=json["StatoWiFi"] == "1",
dry_level=int(json["DryLev"]),
dry_level_selected=int(json["DryingManagerLevel"]),
refresh=json["Refresh"] == "1",
need_clean_filter=json["CleanFilter"] == "1",
full_water_tank=json["WaterTankFull"] == "1",
drylevel_selected=int(json["DryingManagerLevel"]),
door_close=json["DoorState"] == "1",

water_tank_full=json["WaterTankFull"] == "1",
door_closed=json["DoorState"] == "1",
)


Expand Down
6 changes: 3 additions & 3 deletions custom_components/candy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ def extra_state_attributes(self) -> Mapping[str, Any]:
"remaining_minutes": status.remaining_minutes,
"remote_control": status.remote_control,
"dry_level": status.dry_level,
"dry_level_now": status.drylevel_selected,
"dry_level_now": status.dry_level_selected,
"refresh": status.refresh,
"need_clean_filter": status.need_clean_filter,
"watertank_full": status.full_water_tank,
"door_close": status.door_close,
"water_tank_full": status.water_tank_full,
"door_closed": status.door_closed,
}

return attributes
Expand Down
29 changes: 29 additions & 0 deletions tests/fixtures/tumble_dryer/running.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"statusTD": {
"StatoWiFi": "1",
"StatoTD": "2",
"CodiceErrore": "0",
"Pr": "1",
"PrPh": "2",
"RemTime": "150",
"DryLev": "2",
"Time": "0",
"Rapido": "0",
"Opt1": "1",
"Opt2": "1",
"Opt3": "0",
"Opt4": "0",
"Opt5": "0",
"Opt6": "0",
"Opt7": "0",
"Opt8": "0",
"Refresh": "0",
"CleanFilter": "0",
"WaterTankFull": "0",
"DryingManagerLevel": "1",
"DelVal": "0",
"DoorState": "1",
"RecipeId": "NULL",
"CheckUpState": "0"
}
}
100 changes: 98 additions & 2 deletions tests/test_sensor_tumble_dryer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,89 @@ async def test_main_sensor_idle(hass: HomeAssistant, aioclient_mock: AiohttpClie
'dry_level_now': 1,
'refresh': False,
'need_clean_filter': False,
'watertank_full': False,
'door_close': True,
'water_tank_full': False,
'door_closed': True,
'friendly_name': 'Tumble dryer',
'icon': 'mdi:tumble-dryer'
}


async def test_main_sensor_running(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker):
await init_integration(hass, aioclient_mock, load_fixture("tumble_dryer/running.json"))

state = hass.states.get("sensor.tumble_dryer")

assert state
assert state.state == "Running"
assert state.attributes == {
'program': 1,
'remaining_minutes': 150,
'remote_control': True,
'dry_level': 2,
'dry_level_now': 1,
'refresh': False,
'need_clean_filter': False,
'water_tank_full': False,
'door_closed': True,
'friendly_name': 'Tumble dryer',
'icon': 'mdi:tumble-dryer'
}


async def test_cycle_sensor_idle(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker):
await init_integration(hass, aioclient_mock, load_fixture("tumble_dryer/idle.json"))

state = hass.states.get("sensor.dryer_cycle_status")

assert state
assert state.state == "Stopped"
assert state.attributes == {
"friendly_name": "Dryer cycle status",
"icon": "mdi:tumble-dryer"
}


async def test_cycle_sensor_running(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker):
await init_integration(hass, aioclient_mock, load_fixture("tumble_dryer/running.json"))

state = hass.states.get("sensor.dryer_cycle_status")

assert state
assert state.state == "Running"
assert state.attributes == {
"friendly_name": "Dryer cycle status",
"icon": "mdi:tumble-dryer"
}


async def test_remaining_time_sensor_idle(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker):
await init_integration(hass, aioclient_mock, load_fixture("tumble_dryer/idle.json"))

state = hass.states.get("sensor.dryer_cycle_remaining_time")

assert state
assert state.state == "0"
assert state.attributes == {
"friendly_name": "Dryer cycle remaining time",
"icon": "mdi:progress-clock",
"unit_of_measurement": "min",
}


async def test_remaining_time_sensor_running(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker):
await init_integration(hass, aioclient_mock, load_fixture("tumble_dryer/running.json"))

state = hass.states.get("sensor.dryer_cycle_remaining_time")

assert state
assert state.state == "150"
assert state.attributes == {
"friendly_name": "Dryer cycle remaining time",
"icon": "mdi:progress-clock",
"unit_of_measurement": "min",
}


async def test_main_sensor_device_info(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker):
await init_integration(hass, aioclient_mock, load_fixture("tumble_dryer/idle.json"))

Expand All @@ -41,3 +117,23 @@ async def test_main_sensor_device_info(hass: HomeAssistant, aioclient_mock: Aioh
assert device.manufacturer == "Candy"
assert device.name == "Tumble dryer"
assert device.suggested_area == "Bathroom"


async def test_sensors_device_info(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker):
await init_integration(hass, aioclient_mock, load_fixture("tumble_dryer/idle.json"))

er = entity_registry.async_get(hass)
dr = device_registry.async_get(hass)

main_sensor = er.async_get("sensor.tumble_dryer")
cycle_sensor = er.async_get("sensor.dryer_cycle_status")
time_sensor = er.async_get("sensor.dryer_cycle_remaining_time")

main_device = dr.async_get(main_sensor.device_id)
cycle_device = dr.async_get(cycle_sensor.device_id)
time_device = dr.async_get(time_sensor.device_id)

assert main_device
assert cycle_device
assert time_device
assert main_device == cycle_device == time_device

0 comments on commit 1891ed2

Please sign in to comment.