From 56246056cec1dcb1af35b9687c14bfe8814cece9 Mon Sep 17 00:00:00 2001 From: Eric Severance Date: Thu, 26 Aug 2021 01:27:06 -0700 Subject: [PATCH] Be tolerant of Wemo insight_param keys that might not exist (#55232) --- homeassistant/components/wemo/sensor.py | 6 ++++-- homeassistant/components/wemo/switch.py | 16 +++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/wemo/sensor.py b/homeassistant/components/wemo/sensor.py index d1a15ecec3a..654ac92df56 100644 --- a/homeassistant/components/wemo/sensor.py +++ b/homeassistant/components/wemo/sensor.py @@ -78,7 +78,9 @@ class InsightCurrentPower(InsightSensor): def native_value(self) -> StateType: """Return the current power consumption.""" return ( - convert(self.wemo.insight_params[self.entity_description.key], float, 0.0) + convert( + self.wemo.insight_params.get(self.entity_description.key), float, 0.0 + ) / 1000.0 ) @@ -98,6 +100,6 @@ class InsightTodayEnergy(InsightSensor): def native_value(self) -> StateType: """Return the current energy use today.""" miliwatts = convert( - self.wemo.insight_params[self.entity_description.key], float, 0.0 + self.wemo.insight_params.get(self.entity_description.key), float, 0.0 ) return round(miliwatts / (1000.0 * 1000.0 * 60), 2) diff --git a/homeassistant/components/wemo/switch.py b/homeassistant/components/wemo/switch.py index 46e143902f9..a9ee2579c47 100644 --- a/homeassistant/components/wemo/switch.py +++ b/homeassistant/components/wemo/switch.py @@ -76,16 +76,17 @@ class WemoSwitch(WemoEntity, SwitchEntity): if isinstance(self.wemo, Insight): attr["on_latest_time"] = WemoSwitch.as_uptime( - self.wemo.insight_params["onfor"] + self.wemo.insight_params.get("onfor", 0) ) attr["on_today_time"] = WemoSwitch.as_uptime( - self.wemo.insight_params["ontoday"] + self.wemo.insight_params.get("ontoday", 0) ) attr["on_total_time"] = WemoSwitch.as_uptime( - self.wemo.insight_params["ontotal"] + self.wemo.insight_params.get("ontotal", 0) ) attr["power_threshold_w"] = ( - convert(self.wemo.insight_params["powerthreshold"], float, 0.0) / 1000.0 + convert(self.wemo.insight_params.get("powerthreshold"), float, 0.0) + / 1000.0 ) if isinstance(self.wemo, CoffeeMaker): @@ -106,14 +107,15 @@ class WemoSwitch(WemoEntity, SwitchEntity): """Return the current power usage in W.""" if isinstance(self.wemo, Insight): return ( - convert(self.wemo.insight_params["currentpower"], float, 0.0) / 1000.0 + convert(self.wemo.insight_params.get("currentpower"), float, 0.0) + / 1000.0 ) @property def today_energy_kwh(self): """Return the today total energy usage in kWh.""" if isinstance(self.wemo, Insight): - miliwatts = convert(self.wemo.insight_params["todaymw"], float, 0.0) + miliwatts = convert(self.wemo.insight_params.get("todaymw"), float, 0.0) return round(miliwatts / (1000.0 * 1000.0 * 60), 2) @property @@ -122,7 +124,7 @@ class WemoSwitch(WemoEntity, SwitchEntity): if isinstance(self.wemo, CoffeeMaker): return self.wemo.mode_string if isinstance(self.wemo, Insight): - standby_state = int(self.wemo.insight_params["state"]) + standby_state = int(self.wemo.insight_params.get("state", 0)) if standby_state == WEMO_ON: return STATE_ON if standby_state == WEMO_OFF: