Simplify getting alias from tplink smartplug.context (#28696)
* tplink: simplify getting alias from smartplug.context * add a doc-string to private propertypull/29090/head
parent
99c7608fb4
commit
db425f5432
homeassistant/components/tplink
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue