From 71e8ccf9652dd93f2f52bdd803257346f55786c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliv=C3=A9r=20Falvai?= Date: Sun, 28 Nov 2021 17:53:08 +0100 Subject: [PATCH] Handle missing TimeProgr oven field (#50) --- custom_components/candy/client/model.py | 4 ++-- custom_components/candy/sensor.py | 11 +++++++---- tests/fixtures/oven/no_timeprogr.json | 23 +++++++++++++++++++++++ tests/test_sensor_oven.py | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/oven/no_timeprogr.json diff --git a/custom_components/candy/client/model.py b/custom_components/candy/client/model.py index fcbf1e8..6170726 100644 --- a/custom_components/candy/client/model.py +++ b/custom_components/candy/client/model.py @@ -186,7 +186,7 @@ class OvenStatus: selection: int temp: float temp_reached: bool - program_length_minutes: int + program_length_minutes: Optional[int] remote_control: bool @classmethod @@ -197,7 +197,7 @@ def from_json(cls, json): selection=int(json["Selettore"]), temp=round(fahrenheit_to_celsius(int(json["TempRead"]))), temp_reached=json["TempSetRaggiunta"] == "1", - program_length_minutes=int(json["TimeProgr"]), + program_length_minutes=int(json["TimeProgr"]) if "TimeProgr" in json else None, remote_control=json["StatoWiFi"] == "1", ) diff --git a/custom_components/candy/sensor.py b/custom_components/candy/sensor.py index 3400f42..0361374 100644 --- a/custom_components/candy/sensor.py +++ b/custom_components/candy/sensor.py @@ -3,7 +3,8 @@ from homeassistant.helpers.typing import StateType from .client import WashingMachineStatus -from .client.model import MachineState, TumbleDryerStatus, DryerProgramState, OvenStatus, DishwasherStatus, DishwasherState +from .client.model import MachineState, TumbleDryerStatus, DryerProgramState, OvenStatus, DishwasherStatus, \ + DishwasherState from .const import * from homeassistant.components.sensor import SensorEntity from homeassistant.config_entries import ConfigEntry @@ -235,9 +236,9 @@ def unique_id(self) -> str: def state(self) -> StateType: status: TumbleDryerStatus = self.coordinator.data if status.program_state in [DryerProgramState.STOPPED]: - return str(status.cycle_state) + return str(status.cycle_state) else: - return str(status.program_state) + return str(status.program_state) @property def icon(self) -> str: @@ -311,10 +312,12 @@ def extra_state_attributes(self) -> Mapping[str, Any]: "selection": status.selection, "temperature": status.temp, "temperature_reached": status.temp_reached, - "program_length_minutes": status.program_length_minutes, "remote_control": status.remote_control, } + if status.program_length_minutes is not None: + attributes["program_length_minutes"] = status.program_length_minutes + return attributes diff --git a/tests/fixtures/oven/no_timeprogr.json b/tests/fixtures/oven/no_timeprogr.json new file mode 100644 index 0000000..f6c69cd --- /dev/null +++ b/tests/fixtures/oven/no_timeprogr.json @@ -0,0 +1,23 @@ +{ + "statusForno": { + "StatoWiFi": "0", + "CodiceErrore": "E0", + "RecipeId": "0", + "RecipeStep": "0", + "StartStop": "0", + "Pausa": "0", + "SicurezzaBambini": "0", + "Selettore": "0", + "Program": "0", + "TempSet": "0", + "TempRead": "220", + "TempSetRaggiunta": "0", + "DelayStart": "0", + "RemainingTimeProgr": "65535", + "ora": "10", + "min": "46", + "sec": "16", + "FWver": "001A", + "ts": "0" + } +} \ No newline at end of file diff --git a/tests/test_sensor_oven.py b/tests/test_sensor_oven.py index 872c6c3..51412cd 100644 --- a/tests/test_sensor_oven.py +++ b/tests/test_sensor_oven.py @@ -45,6 +45,24 @@ async def test_main_sensor_heating(hass: HomeAssistant, aioclient_mock: AiohttpC } +async def test_main_sensor_no_timeprogr(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker): + await init_integration(hass, aioclient_mock, load_fixture("oven/no_timeprogr.json")) + + state = hass.states.get("sensor.oven") + + assert state + assert state.state == "Idle" + assert state.attributes == { + "program": 0, + "selection": 0, + "temperature": 104, + "temperature_reached": False, + "remote_control": False, + "friendly_name": "Oven", + "icon": "mdi:stove" + } + + async def test_temp_sensor_heating(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker): await init_integration(hass, aioclient_mock, load_fixture("oven/heating.json"))