Simplify getting alias from tplink smartplug.context ()

* tplink: simplify getting alias from smartplug.context

* add a doc-string to private property
pull/29090/head
Bas Nijholt 2019-11-26 08:03:38 +01:00 committed by Paulus Schoutsen
parent 99c7608fb4
commit db425f5432
1 changed files with 8 additions and 10 deletions
homeassistant/components/tplink

View File

@ -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()