Optimize I/O access (#59)

* Optimize I/O access

A single update() will now fetch information from all interesting modules,
including the current device state and the emeter information.

In practice, this will allow dropping the number of I/O reqs per homeassistant update cycle to 1,
which is paramount at least for bulbs which are very picky about sequential accesses.
This can be further extend to other modules/methods, if needed.

Currently fetched data:
* sysinfo
* realtime, today's and this months emeter stats

New properties:
* emeter_realtime: return the most recent emeter information, update()-version of get_emeter_realtime()
* emeter_today: returning today's energy consumption
* emeter_this_month: same for this month

Other changes:
* Accessing @requires_update properties will cause SmartDeviceException if the device has not ever been update()d
* Fix __repr__ for devices that haven't been updated
* Smartbulb uses now the state data from get_sysinfo instead of separately querying the bulb service
* SmartStrip's state_information no longer lists onsince for separate plugs
* The above mentioned properties are now printed out by cli
* Simplify is_on handling for bulbs

* remove implicit updates, return device responses for actions, update README.md instructions. fixes #61
This commit is contained in:
Teemu R
2020-05-24 17:57:54 +02:00
committed by GitHub
parent 012436c494
commit 836f1701b9
10 changed files with 201 additions and 101 deletions

View File

@@ -146,12 +146,11 @@ class SmartStrip(SmartDevice):
:return: Strip information dict, keys in user-presentable form.
:rtype: dict
"""
state: Dict[str, Any] = {"LED state": self.led}
for plug in self.plugs:
if plug.is_on:
state["Plug %s on since" % str(plug)] = self.on_since
return state
return {
"LED state": self.led,
"Childs count": len(self.plugs),
"On since": self.on_since,
}
async def current_consumption(self) -> float:
"""Get the current power consumption in watts.
@@ -218,6 +217,7 @@ class SmartStrip(SmartDevice):
plug_emeter_monthly = await plug.get_emeter_monthly(year=year, kwh=kwh)
for month, value in plug_emeter_monthly:
emeter_monthly[month] += value
return emeter_monthly
@requires_update
@@ -245,7 +245,8 @@ class SmartStripPlug(SmartPlug):
self.parent = parent
self.child_id = child_id
self._sys_info = {**self.parent.sys_info, **self._get_child_info()}
self._last_update = parent._last_update
self._sys_info = parent._sys_info
async def update(self):
"""Override the update to no-op and inform the user."""