mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Simplify getting alias from tplink smartplug.context (#28696)
* tplink: simplify getting alias from smartplug.context * add a doc-string to private property
This commit is contained in:
parent
99c7608fb4
commit
db425f5432
@ -115,6 +115,12 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
"""Return the state attributes of the device."""
|
"""Return the state attributes of the device."""
|
||||||
return self._emeter_params
|
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):
|
def update(self):
|
||||||
"""Update the TP-Link switch's state."""
|
"""Update the TP-Link switch's state."""
|
||||||
try:
|
try:
|
||||||
@ -126,21 +132,13 @@ class SmartPlugSwitch(SwitchDevice):
|
|||||||
self._alias = self.smartplug.alias
|
self._alias = self.smartplug.alias
|
||||||
self._device_id = self._mac
|
self._device_id = self._mac
|
||||||
else:
|
else:
|
||||||
self._alias = [
|
self._alias = self._plug_from_context["alias"]
|
||||||
child
|
|
||||||
for child in self.smartplug.sys_info["children"]
|
|
||||||
if child["id"] == self.smartplug.context
|
|
||||||
][0]["alias"]
|
|
||||||
self._device_id = self.smartplug.context
|
self._device_id = self.smartplug.context
|
||||||
|
|
||||||
if self.smartplug.context is None:
|
if self.smartplug.context is None:
|
||||||
self._state = self.smartplug.state == self.smartplug.SWITCH_STATE_ON
|
self._state = self.smartplug.state == self.smartplug.SWITCH_STATE_ON
|
||||||
else:
|
else:
|
||||||
self._state = [
|
self._state = self._plug_from_context["state"] == 1
|
||||||
child
|
|
||||||
for child in self.smartplug.sys_info["children"]
|
|
||||||
if child["id"] == self.smartplug.context
|
|
||||||
][0]["state"] == 1
|
|
||||||
|
|
||||||
if self.smartplug.has_emeter:
|
if self.smartplug.has_emeter:
|
||||||
emeter_readings = self.smartplug.get_emeter_realtime()
|
emeter_readings = self.smartplug.get_emeter_realtime()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user