Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve categorization of features #904

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

Merged
merged 6 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 2 additions & 7 deletions kasa/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Category(Enum):

#: Device instance required for getting and setting values
device: Device
#: Identifier
id: str
#: User-friendly short description
name: str
#: Name of the property that allows accessing the value
Expand Down Expand Up @@ -99,15 +101,8 @@ class Category(Enum):
#: If set, this property will be used to set *choices*.
choices_getter: str | None = None

#: Identifier
id: str | None = None

def __post_init__(self):
"""Handle late-binding of members."""
# Set id, if unset
if self.id is None:
self.id = self.name.lower().replace(" ", "_")

# Populate minimum & maximum values, if range_getter is given
container = self.container if self.container is not None else self.device
if self.range_getter is not None:
Expand Down
2 changes: 2 additions & 0 deletions kasa/iot/iotbulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ async def _initialize_features(self):
self._add_feature(
Feature(
device=self,
id="brightness",
name="Brightness",
attribute_getter="brightness",
attribute_setter="set_brightness",
Expand All @@ -227,6 +228,7 @@ async def _initialize_features(self):
self._add_feature(
Feature(
device=self,
id="color_temperature",
name="Color temperature",
container=self,
attribute_getter="color_temp",
Expand Down
2 changes: 2 additions & 0 deletions kasa/iot/iotdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ async def _initialize_features(self):
self._add_feature(
Feature(
device=self,
id="rssi",
name="RSSI",
attribute_getter="rssi",
icon="mdi:signal",
Expand All @@ -344,6 +345,7 @@ async def _initialize_features(self):
self._add_feature(
Feature(
device=self,
id="on_since",
name="On since",
attribute_getter="on_since",
icon="mdi:clock",
Expand Down
3 changes: 3 additions & 0 deletions kasa/iot/iotdimmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,15 @@ async def _initialize_features(self):
self._add_feature(
Feature(
device=self,
id="brightness",
name="Brightness",
attribute_getter="brightness",
attribute_setter="set_brightness",
minimum_value=1,
maximum_value=100,
unit="%",
type=Feature.Type.Number,
category=Feature.Category.Primary,
)
)

Expand Down
1 change: 1 addition & 0 deletions kasa/iot/iotplug.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ async def _initialize_features(self):
self._add_feature(
Feature(
device=self,
id="led",
name="LED",
icon="mdi:led-{state}",
attribute_getter="led",
Expand Down
3 changes: 3 additions & 0 deletions kasa/iot/modules/ambientlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ def __init__(self, device, module):
Feature(
device=device,
container=self,
id="ambient_light",
name="Ambient Light",
icon="mdi:brightness-percent",
attribute_getter="ambientlight_brightness",
type=Feature.Type.Sensor,
category=Feature.Category.Primary,
unit="%",
)
)

Expand Down
2 changes: 2 additions & 0 deletions kasa/iot/modules/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ def __init__(self, device, module):
Feature(
device=device,
container=self,
id="cloud_connection",
name="Cloud connection",
icon="mdi:cloud",
attribute_getter="is_connected",
type=Feature.Type.BinarySensor,
category=Feature.Category.Info,
)
)

Expand Down
7 changes: 7 additions & 0 deletions kasa/iot/modules/emeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, device: Device, module: str):
unit="W",
id="current_power_w", # for homeassistant backwards compat
precision_hint=1,
category=Feature.Category.Primary,
)
)
self._add_feature(
Expand All @@ -35,16 +36,19 @@ def __init__(self, device: Device, module: str):
unit="kWh",
id="today_energy_kwh", # for homeassistant backwards compat
precision_hint=3,
category=Feature.Category.Info,
)
)
self._add_feature(
Feature(
device,
id="consumption_this_month",
name="This month's consumption",
attribute_getter="emeter_this_month",
container=self,
unit="kWh",
precision_hint=3,
category=Feature.Category.Info,
)
)
self._add_feature(
Expand All @@ -56,6 +60,7 @@ def __init__(self, device: Device, module: str):
unit="kWh",
id="total_energy_kwh", # for homeassistant backwards compat
precision_hint=3,
category=Feature.Category.Info,
)
)
self._add_feature(
Expand All @@ -67,6 +72,7 @@ def __init__(self, device: Device, module: str):
unit="V",
id="voltage", # for homeassistant backwards compat
precision_hint=1,
category=Feature.Category.Primary,
)
)
self._add_feature(
Expand All @@ -78,6 +84,7 @@ def __init__(self, device: Device, module: str):
unit="A",
id="current_a", # for homeassistant backwards compat
precision_hint=2,
category=Feature.Category.Primary,
)
)

Expand Down
12 changes: 4 additions & 8 deletions kasa/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ def _initialize_features(self): # noqa: B027

def _add_feature(self, feature: Feature):
"""Add module feature."""

def _slugified_name(name):
return name.lower().replace(" ", "_").replace("'", "_")

feat_name = _slugified_name(feature.name)
if feat_name in self._module_features:
raise KasaException("Duplicate name detected %s" % feat_name)
self._module_features[feat_name] = feature
id_ = feature.id
if id_ in self._module_features:
raise KasaException("Duplicate id detected %s" % id_)
self._module_features[id_] = feature

def __repr__(self) -> str:
return (
Expand Down
18 changes: 12 additions & 6 deletions kasa/smart/modules/alarmmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def _initialize_features(self):
self._add_feature(
Feature(
device,
"Alarm",
id="alarm",
name="Alarm",
container=self,
attribute_getter="active",
icon="mdi:bell",
Expand All @@ -37,7 +38,8 @@ def _initialize_features(self):
self._add_feature(
Feature(
device,
"Alarm source",
id="alarm_source",
name="Alarm source",
container=self,
attribute_getter="source",
icon="mdi:bell",
Expand All @@ -46,7 +48,8 @@ def _initialize_features(self):
self._add_feature(
Feature(
device,
"Alarm sound",
id="alarm_sound",
name="Alarm sound",
container=self,
attribute_getter="alarm_sound",
attribute_setter="set_alarm_sound",
Expand All @@ -58,7 +61,8 @@ def _initialize_features(self):
self._add_feature(
Feature(
device,
"Alarm volume",
id="alarm_volume",
name="Alarm volume",
container=self,
attribute_getter="alarm_volume",
attribute_setter="set_alarm_volume",
Expand All @@ -70,7 +74,8 @@ def _initialize_features(self):
self._add_feature(
Feature(
device,
"Test alarm",
id="test_alarm",
name="Test alarm",
container=self,
attribute_setter="play",
type=Feature.Type.Action,
Expand All @@ -79,7 +84,8 @@ def _initialize_features(self):
self._add_feature(
Feature(
device,
"Stop alarm",
id="stop_alarm",
name="Stop alarm",
container=self,
attribute_setter="stop",
type=Feature.Type.Action,
Expand Down
13 changes: 10 additions & 3 deletions kasa/smart/modules/autooffmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"Auto off enabled",
id="auto_off_enabled",
name="Auto off enabled",
container=self,
attribute_getter="enabled",
attribute_setter="set_enabled",
Expand All @@ -33,7 +34,8 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"Auto off minutes",
id="auto_off_minutes",
name="Auto off minutes",
container=self,
attribute_getter="delay",
attribute_setter="set_delay",
Expand All @@ -42,7 +44,12 @@ def __init__(self, device: SmartDevice, module: str):
)
self._add_feature(
Feature(
device, "Auto off at", container=self, attribute_getter="auto_off_at"
device,
id="auto_off_at",
name="Auto off at",
container=self,
attribute_getter="auto_off_at",
category=Feature.Category.Info,
)
)

Expand Down
5 changes: 5 additions & 0 deletions kasa/smart/modules/battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"battery_level",
"Battery level",
container=self,
attribute_getter="battery",
icon="mdi:battery",
unit="%",
category=Feature.Category.Info,
)
)
self._add_feature(
Feature(
device,
"battery_low",
"Battery low",
container=self,
attribute_getter="battery_low",
icon="mdi:alert",
type=Feature.Type.BinarySensor,
category=Feature.Category.Debug,
)
)

Expand Down
3 changes: 2 additions & 1 deletion kasa/smart/modules/brightness.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"Brightness",
id="brightness",
name="Brightness",
container=self,
attribute_getter="brightness",
attribute_setter="set_brightness",
Expand Down
4 changes: 3 additions & 1 deletion kasa/smart/modules/cloudmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"Cloud connection",
id="cloud_connection",
name="Cloud connection",
container=self,
attribute_getter="is_connected",
icon="mdi:cloud",
type=Feature.Type.BinarySensor,
category=Feature.Category.Info,
)
)

Expand Down
1 change: 1 addition & 0 deletions kasa/smart/modules/colormodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"hsv",
"HSV",
container=self,
attribute_getter="hsv",
Expand Down
1 change: 1 addition & 0 deletions kasa/smart/modules/colortemp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"color_temperature",
"Color temperature",
container=self,
attribute_getter="color_temp",
Expand Down
6 changes: 6 additions & 0 deletions kasa/smart/modules/energymodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,37 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"consumption_current",
name="Current consumption",
attribute_getter="current_power",
container=self,
unit="W",
precision_hint=1,
category=Feature.Category.Primary,
)
)
self._add_feature(
Feature(
device,
"consumption_today",
name="Today's consumption",
attribute_getter="emeter_today",
container=self,
unit="Wh",
precision_hint=2,
category=Feature.Category.Info,
)
)
self._add_feature(
Feature(
device,
"consumption_this_month",
name="This month's consumption",
attribute_getter="emeter_this_month",
container=self,
unit="Wh",
precision_hint=2,
category=Feature.Category.Info,
)
)

Expand Down
6 changes: 4 additions & 2 deletions kasa/smart/modules/fanmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"Fan speed level",
id="fan_speed_level",
name="Fan speed level",
container=self,
attribute_getter="fan_speed_level",
attribute_setter="set_fan_speed_level",
Expand All @@ -36,7 +37,8 @@ def __init__(self, device: SmartDevice, module: str):
self._add_feature(
Feature(
device,
"Fan sleep mode",
id="fan_sleep_mode",
name="Fan sleep mode",
container=self,
attribute_getter="sleep_mode",
attribute_setter="set_sleep_mode",
Expand Down
Loading