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

Rename SettingDescriptor's type to setting_type #1715

Merged
merged 4 commits into from
Feb 6, 2023
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
20 changes: 10 additions & 10 deletions miio/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Descriptor:

id: str
name: str
type: Optional[type] = None
extras: Dict = attr.ib(factory=dict, repr=False)


@attr.s(auto_attribs=True)
Expand All @@ -40,10 +42,9 @@ class ActionDescriptor(Descriptor):
method_name: Optional[str] = attr.ib(default=None, repr=False)
method: Optional[Callable] = attr.ib(default=None, repr=False)
inputs: Optional[List[Any]] = attr.ib(default=None, repr=True)
extras: Dict = attr.ib(factory=dict, repr=False)


@attr.s(auto_attribs=True)
@attr.s(auto_attribs=True, kw_only=True)
class SensorDescriptor(Descriptor):
"""Describes a sensor exposed by the device.

Expand All @@ -54,9 +55,7 @@ class SensorDescriptor(Descriptor):
"""

property: str
type: type
unit: Optional[str] = None
extras: Dict = attr.ib(factory=dict, repr=False)


class SettingType(Enum):
Expand All @@ -72,10 +71,9 @@ class SettingDescriptor(Descriptor):

property: str
unit: Optional[str] = None
type = SettingType.Undefined
setting_type = SettingType.Undefined
setter: Optional[Callable] = attr.ib(default=None, repr=False)
setter_name: Optional[str] = attr.ib(default=None, repr=False)
extras: Dict = attr.ib(factory=dict, repr=False)

def cast_value(self, value: int):
"""Casts value to the expected type."""
Expand All @@ -84,21 +82,22 @@ def cast_value(self, value: int):
SettingType.Enum: int,
SettingType.Number: int,
}
return cast_map[self.type](int(value))
return cast_map[self.setting_type](int(value))


@attr.s(auto_attribs=True, kw_only=True)
class BooleanSettingDescriptor(SettingDescriptor):
"""Presents a settable boolean value."""

type: SettingType = SettingType.Boolean
type: type = bool
setting_type: SettingType = SettingType.Boolean


@attr.s(auto_attribs=True, kw_only=True)
class EnumSettingDescriptor(SettingDescriptor):
"""Presents a settable, enum-based value."""

type: SettingType = SettingType.Enum
setting_type: SettingType = SettingType.Enum
choices_attribute: Optional[str] = attr.ib(default=None, repr=False)
choices: Optional[Type[Enum]] = attr.ib(default=None, repr=False)

Expand All @@ -115,4 +114,5 @@ class NumberSettingDescriptor(SettingDescriptor):
max_value: int
step: int
range_attribute: Optional[str] = attr.ib(default=None)
type: SettingType = SettingType.Number
type: type = int
setting_type: SettingType = SettingType.Number
24 changes: 16 additions & 8 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,29 @@ def _setting_descriptors_from_status(
raise Exception(
f"Neither setter or setter_name was defined for {setting}"
)
setting = cast(EnumSettingDescriptor, setting)
if (
setting.type == SettingType.Enum
and setting.choices_attribute is not None
):
retrieve_choices_function = getattr(self, setting.choices_attribute)
setting.choices = retrieve_choices_function()
if setting.type == SettingType.Number:

if setting.setting_type == SettingType.Enum:
setting = cast(EnumSettingDescriptor, setting)
if setting.choices_attribute is not None:
retrieve_choices_function = getattr(self, setting.choices_attribute)
setting.choices = retrieve_choices_function()

elif setting.setting_type == SettingType.Number:
setting = cast(NumberSettingDescriptor, setting)
if setting.range_attribute is not None:
range_def = getattr(self, setting.range_attribute)
setting.min_value = range_def.min_value
setting.max_value = range_def.max_value
setting.step = range_def.step

elif setting.setting_type == SettingType.Boolean:
pass # just to exhaust known types

else:
raise NotImplementedError(
"Unknown setting type: %s" % setting.setting_type
)

return settings

def _sensor_descriptors_from_status(
Expand Down
4 changes: 4 additions & 0 deletions miio/miot_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def convert_type(cls, input: str):
"bool": bool,
"string": str,
"float": float,
"none": None,
}
return type_map[input]

Expand Down Expand Up @@ -303,6 +304,7 @@ def _descriptor_for_choices(self) -> Union[SensorDescriptor, EnumSettingDescript
unit=self.unit,
choices=choices,
extras=self.extras,
type=self.format,
)
return desc
else:
Expand All @@ -322,6 +324,7 @@ def _descriptor_for_ranged(
step=self.range[2],
unit=self.unit,
extras=self.extras,
type=self.format,
)
return desc
else:
Expand All @@ -335,6 +338,7 @@ def _create_boolean_setting(self) -> BooleanSettingDescriptor:
property=self.name,
unit=self.unit,
extras=self.extras,
type=bool,
)

def _create_sensor(self) -> SensorDescriptor:
Expand Down