diff --git a/homeassistant/components/tplink/switch.py b/homeassistant/components/tplink/switch.py index 791d358c509..b6ca69f4ccd 100644 --- a/homeassistant/components/tplink/switch.py +++ b/homeassistant/components/tplink/switch.py @@ -115,6 +115,12 @@ class SmartPlugSwitch(SwitchDevice): """Return the state attributes of the device.""" return self._emeter_params + @property + def _plug_from_context(self): + """Return the plug from the context.""" + children = self.smartplug.sys_info["children"] + return next(c for c in children if c["id"] == self.smartplug.context) + def update(self): """Update the TP-Link switch's state.""" try: @@ -126,21 +132,13 @@ class SmartPlugSwitch(SwitchDevice): self._alias = self.smartplug.alias self._device_id = self._mac else: - self._alias = [ - child - for child in self.smartplug.sys_info["children"] - if child["id"] == self.smartplug.context - ][0]["alias"] + self._alias = self._plug_from_context["alias"] self._device_id = self.smartplug.context if self.smartplug.context is None: self._state = self.smartplug.state == self.smartplug.SWITCH_STATE_ON else: - self._state = [ - child - for child in self.smartplug.sys_info["children"] - if child["id"] == self.smartplug.context - ][0]["state"] == 1 + self._state = self._plug_from_context["state"] == 1 if self.smartplug.has_emeter: emeter_readings = self.smartplug.get_emeter_realtime()