Skip to content

Commit

Permalink
Handle missing TimeProgr oven field (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofalvai authored Nov 28, 2021
1 parent 82bb365 commit 71e8ccf
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
4 changes: 2 additions & 2 deletions custom_components/candy/client/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
)

Expand Down
11 changes: 7 additions & 4 deletions custom_components/candy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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


Expand Down
23 changes: 23 additions & 0 deletions tests/fixtures/oven/no_timeprogr.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
18 changes: 18 additions & 0 deletions tests/test_sensor_oven.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down

0 comments on commit 71e8ccf

Please sign in to comment.