From db425f54326d19009de0c80d18e2a630a22fab36 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 26 Nov 2019 08:03:38 +0100 Subject: [PATCH] Simplify getting alias from tplink smartplug.context (#28696) * tplink: simplify getting alias from smartplug.context * add a doc-string to private property --- homeassistant/components/tplink/switch.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) 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()