Embed FeatureType inside Feature (#860)

Moves `FeatureType` into `Feature` to make it easier to use the API.
This also enforces that no invalid types are accepted (i.e.,
`Category.Config` cannot be a `Sensor`)
If `--verbose` is used with the cli tool, some extra information is
displayed for features when in the state command.
This commit is contained in:
Teemu R
2024-04-24 18:38:52 +02:00
committed by GitHub
parent e410e4f3f3
commit 65874c0365
23 changed files with 135 additions and 69 deletions

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -32,7 +32,7 @@ class AlarmModule(SmartModule):
container=self,
attribute_getter="active",
icon="mdi:bell",
type=FeatureType.BinarySensor,
type=Feature.Type.BinarySensor,
)
)
self._add_feature(
@@ -60,7 +60,7 @@ class AlarmModule(SmartModule):
"Test alarm",
container=self,
attribute_setter="play",
type=FeatureType.Action,
type=Feature.Type.Action,
)
)
self._add_feature(
@@ -69,7 +69,7 @@ class AlarmModule(SmartModule):
"Stop alarm",
container=self,
attribute_setter="stop",
type=FeatureType.Action,
type=Feature.Type.Action,
)
)

View File

@@ -27,6 +27,7 @@ class AutoOffModule(SmartModule):
container=self,
attribute_getter="enabled",
attribute_setter="set_enabled",
type=Feature.Type.Switch,
)
)
self._add_feature(
@@ -36,6 +37,7 @@ class AutoOffModule(SmartModule):
container=self,
attribute_getter="delay",
attribute_setter="set_delay",
type=Feature.Type.Number,
)
)
self._add_feature(

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -35,7 +35,7 @@ class BatterySensor(SmartModule):
container=self,
attribute_getter="battery_low",
icon="mdi:alert",
type=FeatureType.BinarySensor,
type=Feature.Type.BinarySensor,
)
)

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -31,7 +31,7 @@ class Brightness(SmartModule):
attribute_setter="set_brightness",
minimum_value=BRIGHTNESS_MIN,
maximum_value=BRIGHTNESS_MAX,
type=FeatureType.Number,
type=Feature.Type.Number,
category=Feature.Category.Primary,
)
)

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...exceptions import SmartErrorCode
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -28,7 +28,7 @@ class CloudModule(SmartModule):
container=self,
attribute_getter="is_connected",
icon="mdi:cloud",
type=FeatureType.BinarySensor,
type=Feature.Type.BinarySensor,
)
)

View File

@@ -25,8 +25,9 @@ class ColorModule(SmartModule):
"HSV",
container=self,
attribute_getter="hsv",
# TODO proper type for setting hsv
attribute_setter="set_hsv",
# TODO proper type for setting hsv
type=Feature.Type.Unknown,
)
)

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -27,7 +27,7 @@ class FanModule(SmartModule):
attribute_getter="fan_speed_level",
attribute_setter="set_fan_speed_level",
icon="mdi:fan",
type=FeatureType.Number,
type=Feature.Type.Number,
minimum_value=1,
maximum_value=4,
category=Feature.Category.Primary,
@@ -41,7 +41,7 @@ class FanModule(SmartModule):
attribute_getter="sleep_mode",
attribute_setter="set_sleep_mode",
icon="mdi:sleep",
type=FeatureType.Switch,
type=Feature.Type.Switch,
)
)

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Any, Optional
from ...exceptions import SmartErrorCode
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
try:
@@ -59,7 +59,7 @@ class Firmware(SmartModule):
container=self,
attribute_getter="auto_update_enabled",
attribute_setter="set_auto_update_enabled",
type=FeatureType.Switch,
type=Feature.Type.Switch,
)
)
self._add_feature(
@@ -68,7 +68,7 @@ class Firmware(SmartModule):
"Update available",
container=self,
attribute_getter="update_available",
type=FeatureType.BinarySensor,
type=Feature.Type.BinarySensor,
)
)

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -34,7 +34,7 @@ class HumiditySensor(SmartModule):
"Humidity warning",
container=self,
attribute_getter="humidity_warning",
type=FeatureType.BinarySensor,
type=Feature.Type.BinarySensor,
icon="mdi:alert",
)
)

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -27,7 +27,7 @@ class LedModule(SmartModule):
icon="mdi:led-{state}",
attribute_getter="led",
attribute_setter="set_led",
type=FeatureType.Switch,
type=Feature.Type.Switch,
category=Feature.Category.Config,
)
)

View File

@@ -5,7 +5,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING
from ...exceptions import KasaException
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -35,7 +35,7 @@ class LightTransitionModule(SmartModule):
icon=icon,
attribute_getter="enabled_v1",
attribute_setter="set_enabled_v1",
type=FeatureType.Switch,
type=Feature.Type.Switch,
)
)
elif self.supported_version >= 2:
@@ -51,7 +51,7 @@ class LightTransitionModule(SmartModule):
attribute_getter="turn_on_transition",
attribute_setter="set_turn_on_transition",
icon=icon,
type=FeatureType.Number,
type=Feature.Type.Number,
maximum_value=self.MAXIMUM_DURATION,
)
) # self._turn_on_transition_max
@@ -63,7 +63,7 @@ class LightTransitionModule(SmartModule):
attribute_getter="turn_off_transition",
attribute_setter="set_turn_off_transition",
icon=icon,
type=FeatureType.Number,
type=Feature.Type.Number,
maximum_value=self.MAXIMUM_DURATION,
)
) # self._turn_off_transition_max

View File

@@ -4,7 +4,7 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Literal
from ...feature import Feature, FeatureType
from ...feature import Feature
from ..smartmodule import SmartModule
if TYPE_CHECKING:
@@ -35,7 +35,7 @@ class TemperatureSensor(SmartModule):
"Temperature warning",
container=self,
attribute_getter="temperature_warning",
type=FeatureType.BinarySensor,
type=Feature.Type.BinarySensor,
icon="mdi:alert",
)
)
@@ -46,6 +46,7 @@ class TemperatureSensor(SmartModule):
container=self,
attribute_getter="temperature_unit",
attribute_setter="set_temperature_unit",
type=Feature.Type.Choice,
)
)
# TODO: use temperature_unit for feature creation

View File

@@ -26,6 +26,7 @@ class TemperatureControl(SmartModule):
attribute_getter="target_temperature",
attribute_setter="set_target_temperature",
icon="mdi:thermometer",
type=Feature.Type.Number,
)
)
# TODO: this might belong into its own module, temperature_correction?
@@ -38,6 +39,7 @@ class TemperatureControl(SmartModule):
attribute_setter="set_temperature_offset",
minimum_value=-10,
maximum_value=10,
type=Feature.Type.Number,
)
)