Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2021.8.8 #54853

Merged
merged 7 commits into from
Aug 18, 2021
Merged

2021.8.8 #54853

Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix tplink doing I/O in event loop and optimize (#54570)
* Optimize tplink i/o

* Cache has_emeter reduceing the number of i/o requests on hs300 by 5
* Use the state from the sysinfo dict for non-strips reducing required requests by one

* Remove I/O from __init__, read has_emeter from sysinfo

* Cleanup __init__ to avoid I/O
* Re-use the sysinfo response for has_emeter
* Use async_add_executor_job() to execute the synchronous I/O ops.

* use the device alias instead of host for coordinator, use executor for unavailable_devices

* Remove unnecessary self.hass assignment

Co-authored-by: Martin Hjelmare <[email protected]>

Co-authored-by: Martin Hjelmare <[email protected]>
  • Loading branch information
2 people authored and balloob committed Aug 18, 2021
commit 01082fb5ab2e2c0318bda9f4fb9b22ece2418355
26 changes: 17 additions & 9 deletions homeassistant/components/tplink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ async def async_retry_devices(self) -> None:

for device in unavailable_devices:
try:
device.get_sysinfo()
await hass.async_add_executor_job(device.get_sysinfo)
except SmartDeviceException:
continue
_LOGGER.debug(
Expand All @@ -175,7 +175,7 @@ async def async_retry_devices(self) -> None:
for switch in switches:

try:
await hass.async_add_executor_job(switch.get_sysinfo)
info = await hass.async_add_executor_job(switch.get_sysinfo)
except SmartDeviceException:
_LOGGER.warning(
"Device at '%s' not reachable during setup, will retry later",
Expand All @@ -186,7 +186,7 @@ async def async_retry_devices(self) -> None:

hass_data[COORDINATORS][
switch.context or switch.mac
] = coordinator = SmartPlugDataUpdateCoordinator(hass, switch)
] = coordinator = SmartPlugDataUpdateCoordinator(hass, switch, info["alias"])
await coordinator.async_config_entry_first_refresh()

if unavailable_devices:
Expand Down Expand Up @@ -222,16 +222,20 @@ def __init__(
self,
hass: HomeAssistant,
smartplug: SmartPlug,
alias: str,
) -> None:
"""Initialize DataUpdateCoordinator to gather data for specific SmartPlug."""
self.smartplug = smartplug

update_interval = timedelta(seconds=30)
super().__init__(
hass, _LOGGER, name=smartplug.alias, update_interval=update_interval
hass,
_LOGGER,
name=alias,
update_interval=update_interval,
)

async def _async_update_data(self) -> dict:
def _update_data(self) -> dict:
"""Fetch all device and sensor data from api."""
try:
info = self.smartplug.sys_info
Expand All @@ -244,9 +248,7 @@ async def _async_update_data(self) -> dict:
if self.smartplug.context is None:
data[CONF_ALIAS] = info["alias"]
data[CONF_DEVICE_ID] = info["mac"]
data[CONF_STATE] = (
self.smartplug.state == self.smartplug.SWITCH_STATE_ON
)
data[CONF_STATE] = bool(info["relay_state"])
else:
plug_from_context = next(
c
Expand All @@ -256,7 +258,9 @@ async def _async_update_data(self) -> dict:
data[CONF_ALIAS] = plug_from_context["alias"]
data[CONF_DEVICE_ID] = self.smartplug.context
data[CONF_STATE] = plug_from_context["state"] == 1
if self.smartplug.has_emeter:

# Check if the device has emeter
if "ENE" in info["feature"]:
emeter_readings = self.smartplug.get_emeter_realtime()
data[CONF_EMETER_PARAMS] = {
ATTR_CURRENT_POWER_W: round(float(emeter_readings["power"]), 2),
Expand Down Expand Up @@ -288,3 +292,7 @@ async def _async_update_data(self) -> dict:

self.name = data[CONF_ALIAS]
return data

async def _async_update_data(self) -> dict:
"""Fetch all device and sensor data from api."""
return await self.hass.async_add_executor_job(self._update_data)