FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Change state_information to return feature values (#804) · python-kasa/python-kasa@35dbda7 · GitHub

Repository navigation

Commit 35dbda7

Browse files
authored
Change state_information to return feature values (#804)
This changes `state_information` to return the names and values of all defined features. It was originally a "temporary" hack to show some extra, device-specific information in the cli tool, but now that we have device-defined features we can leverage them.
1 parent d63f43a commit 35dbda7

13 files changed

Lines changed: 70 additions & 115 deletions

‎kasa/cli.py‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -609,16 +609,7 @@ async def state(ctx, dev: Device):
609609
echo(f"\tMAC (rssi): {dev.mac} ({dev.rssi})")
610610
echo(f"\tLocation: {dev.location}")
611611

612-
echo("\n\t[bold]== Device specific information ==[/bold]")
613-
for info_name, info_data in dev.state_information.items():
614-
if isinstance(info_data, list):
615-
echo(f"\t{info_name}:")
616-
for item in info_data:
617-
echo(f"\t\t{item}")
618-
else:
619-
echo(f"\t{info_name}: {info_data}")
620-
621-
echo("\n\t[bold]== Features == [/bold]")
612+
echo("\n\t[bold]== Device-specific information == [/bold]")
622613
for id_, feature in dev.features.items():
623614
echo(f"\t{feature.name} ({id_}): {feature.value}")
624615

‎kasa/device.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,9 @@ def internal_state(self) -> Any:
304304
"""Return all the internal state data."""
305305

306306
@property
307-
@abstractmethod
308307
def state_information(self) -> Dict[str, Any]:
309-
"""Return the key state information."""
308+
"""Return available features and their values."""
309+
return {feat.name: feat.value for feat in self._features.values()}
310310

311311
@property
312312
def features(self) -> Dict[str, Feature]:

‎kasa/iot/iotbulb.py‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import re
44
from enum import Enum
5-
from typing import Any, Dict, List, Optional, cast
5+
from typing import Dict, List, Optional, cast
66

77
try:
88
from pydantic.v1 import BaseModel, Field, root_validator
@@ -462,23 +462,6 @@ async def set_brightness(
462462
light_state = {"brightness": brightness}
463463
return await self.set_light_state(light_state, transition=transition)
464464

465-
@property # type: ignore
466-
@requires_update
467-
def state_information(self) -> Dict[str, Any]:
468-
"""Return bulb-specific state information."""
469-
info: Dict[str, Any] = {
470-
"Brightness": self.brightness,
471-
"Is dimmable": self.is_dimmable,
472-
}
473-
if self.is_variable_color_temp:
474-
info["Color temperature"] = self.color_temp
475-
info["Valid temperature range"] = self.valid_temperature_range
476-
if self.is_color:
477-
info["HSV"] = self.hsv
478-
info["Presets"] = self.presets
479-
480-
return info
481-
482465
@property # type: ignore
483466
@requires_update
484467
def is_on(self) -> bool:

‎kasa/iot/iotdevice.py‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,6 @@ def on_since(self) -> Optional[datetime]:
615615

616616
return datetime.now().replace(microsecond=0) - timedelta(seconds=on_time)
617617

618-
@property # type: ignore
619-
@requires_update
620-
def state_information(self) -> Dict[str, Any]:
621-
"""Return device-type specific, end-user friendly state information."""
622-
raise NotImplementedError("Device subclass needs to implement this.")
623-
624618
@property # type: ignore
625619
@requires_update
626620
def device_id(self) -> str:

‎kasa/iot/iotdimmer.py‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,3 @@ def is_dimmable(self) -> bool:
232232
"""Whether the switch supports brightness changes."""
233233
sys_info = self.sys_info
234234
return "brightness" in sys_info
235-
236-
@property # type: ignore
237-
@requires_update
238-
def state_information(self) -> Dict[str, Any]:
239-
"""Return switch-specific state information."""
240-
info = super().state_information
241-
info["Brightness"] = self.brightness
242-
243-
return info

‎kasa/iot/iotlightstrip.py‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Module for light strips (KL430)."""
2-
from typing import Any, Dict, List, Optional
2+
from typing import Dict, List, Optional
33

44
from ..device_type import DeviceType
55
from ..deviceconfig import DeviceConfig
@@ -84,18 +84,6 @@ def effect_list(self) -> Optional[List[str]]:
8484
"""
8585
return EFFECT_NAMES_V1 if self.has_effects else None
8686

87-
@property # type: ignore
88-
@requires_update
89-
def state_information(self) -> Dict[str, Any]:
90-
"""Return strip specific state information."""
91-
info = super().state_information
92-
93-
info["Length"] = self.length
94-
if self.has_effects:
95-
info["Effect"] = self.effect["name"]
96-
97-
return info
98-
9987
@requires_update
10088
async def set_effect(
10189
self,

‎kasa/iot/iotplug.py‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Module for smart plugs (HS100, HS110, ..)."""
22
import logging
3-
from typing import Any, Dict, Optional
3+
from typing import Optional
44

55
from ..device_type import DeviceType
66
from ..deviceconfig import DeviceConfig
@@ -99,12 +99,6 @@ async def set_led(self, state: bool):
9999
"system", "set_led_off", {"off": int(not state)}
100100
)
101101

102-
@property # type: ignore
103-
@requires_update
104-
def state_information(self) -> Dict[str, Any]:
105-
"""Return switch-specific state information."""
106-
return {}
107-
108102

109103
class IotWallSwitch(IotPlug):
110104
"""Representation of a TP-Link Smart Wall Switch."""

‎kasa/iot/iotstrip.py‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,6 @@ async def set_led(self, state: bool):
154154
"""Set the state of the led (night mode)."""
155155
await self._query_helper("system", "set_led_off", {"off": int(not state)})
156156

157-
@property # type: ignore
158-
@requires_update
159-
def state_information(self) -> Dict[str, Any]:
160-
"""Return strip-specific state information.
161-
162-
:return: Strip information dict, keys in user-presentable form.
163-
"""
164-
return {
165-
"LED state": self.led,
166-
"Childs count": len(self.children),
167-
"On since": self.on_since,
168-
}
169-
170157
async def current_consumption(self) -> float:
171158
"""Get the current power consumption in watts."""
172159
return sum([await plug.current_consumption() for plug in self.children])

‎kasa/smart/smartbulb.py‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Module for tapo-branded smart bulbs (L5**)."""
2-
from typing import Any, Dict, List, Optional
2+
from typing import Dict, List, Optional
33

44
from ..bulb import Bulb
55
from ..exceptions import KasaException
@@ -238,25 +238,6 @@ async def set_effect(
238238
}
239239
)
240240

241-
@property # type: ignore
242-
def state_information(self) -> Dict[str, Any]:
243-
"""Return bulb-specific state information."""
244-
info: Dict[str, Any] = {
245-
# TODO: re-enable after we don't inherit from smartbulb
246-
# **super().state_information
247-
"Is dimmable": self.is_dimmable,
248-
}
249-
if self.is_dimmable:
250-
info["Brightness"] = self.brightness
251-
if self.is_variable_color_temp:
252-
info["Color temperature"] = self.color_temp
253-
info["Valid temperature range"] = self.valid_temperature_range
254-
if self.is_color:
255-
info["HSV"] = self.hsv
256-
info["Presets"] = self.presets
257-
258-
return info
259-
260241
@property
261242
def presets(self) -> List[BulbPreset]:
262243
"""Return a list of available bulb setting presets."""

‎kasa/smart/smartdevice.py‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,6 @@ def ssid(self) -> str:
334334
ssid = base64.b64decode(ssid).decode() if ssid else "No SSID"
335335
return ssid
336336

337-
@property
338-
def state_information(self) -> Dict[str, Any]:
339-
"""Return the key state information."""
340-
return {
341-
"overheated": self._info.get("overheated"),
342-
"signal_level": self._info.get("signal_level"),
343-
"SSID": self.ssid,
344-
}
345-
346337
@property
347338
def has_emeter(self) -> bool:
348339
"""Return if the device has emeter."""

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL