Skip to content

Commit

Permalink
Rename update_attributes to device_attributes and include all of them
Browse files Browse the repository at this point in the history
  • Loading branch information
sopelj committed Dec 26, 2023
1 parent ff762a0 commit 0cce46c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
13 changes: 8 additions & 5 deletions ember_mug/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,18 @@ def device_type(self) -> DeviceType: # type: ignore[override]
return DeviceType.MUG

@cached_property
def update_attributes(self) -> set[str]:
def device_attributes(self) -> set[str]:
"""Attributes to update based on model and extra."""
attributes = UPDATE_ATTRS
attributes = EXTRA_ATTRS | INITIAL_ATTRS | UPDATE_ATTRS
if not self.model or self.device_type in (DeviceType.CUP, DeviceType.TUMBLER):
# The Cup and Tumbler cannot be named
attributes = attributes - {"name"}
attributes -= {"name"}
elif not self.model or self.device_type == DeviceType.TRAVEL_MUG:
# Tge Travel Mug does not have an LED colour, but has a volume attribute
attributes = (attributes - {"led_colour"}) | {"volume_level"}
if self.model != DeviceModel.TRAVEL_MUG_12_OZ:
# Only Travel mug has this attribute?
attributes -= {"battery_voltage"}
return attributes


Expand Down Expand Up @@ -262,15 +265,15 @@ def get_formatted_attr(self, attr: str) -> str | None:
@property
def formatted(self) -> dict[str, Any]:
"""Return human-readable names and values for all attributes for display."""
all_attrs = INITIAL_ATTRS | self.model_info.update_attributes | {"use_metric"}
all_attrs = self.model_info.device_attributes | {"use_metric"}
if not self.debug:
all_attrs -= EXTRA_ATTRS
return {label: self.get_formatted_attr(attr) for attr, label in ATTR_LABELS.items() if attr in all_attrs}

def as_dict(self) -> dict[str, Any]:
"""Dump all attributes as dict for info/debugging."""
data = {k: asdict(v) if is_dataclass(v) else v for k, v in asdict(self).items()}
all_attrs = self.model_info.update_attributes | INITIAL_ATTRS
all_attrs = self.model_info.device_attributes
if not self.debug:
all_attrs -= EXTRA_ATTRS
data.update(
Expand Down
6 changes: 4 additions & 2 deletions ember_mug/mug.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def can_write(self) -> bool:

def has_attribute(self, attribute: str) -> bool:
"""Check whether the device has the given attribute."""
return attribute in self.data.model_info.update_attributes
return attribute in self.data.model_info.device_attributes

async def _ensure_connection(self) -> None:
"""Connect to mug."""
Expand Down Expand Up @@ -378,7 +378,9 @@ async def update_initial(self) -> list[Change]:

async def update_all(self) -> list[Change]:
"""Update all standard attributes."""
return await self._update_multiple(self.data.model_info.update_attributes)
return await self._update_multiple(
self.data.model_info.device_attributes - INITIAL_ATTRS,
)

async def _update_multiple(self, attrs: set[str]) -> list[Change]:
"""Update a list of attributes from the mug."""
Expand Down
30 changes: 15 additions & 15 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,31 @@ def test_mug_meta() -> None:
def test_mug_model() -> None:
mug = ModelInfo(DeviceModel.MUG_2_10_OZ)
assert mug.device_type == DeviceType.MUG
assert "name" in mug.update_attributes
assert "name" in mug.update_attributes
assert "battery_voltage" not in mug.update_attributes
assert "name" in mug.device_attributes
assert "name" in mug.device_attributes
assert "battery_voltage" not in mug.device_attributes

travel_mug = ModelInfo(DeviceModel.TRAVEL_MUG_12_OZ)
assert travel_mug.device_type == DeviceType.TRAVEL_MUG
assert "name" in travel_mug.update_attributes
assert "name" in travel_mug.update_attributes
assert "volume_level" in travel_mug.update_attributes
assert "name" in travel_mug.device_attributes
assert "name" in travel_mug.device_attributes
assert "volume_level" in travel_mug.device_attributes

tumbler = ModelInfo(DeviceModel.TUMBLER_16_OZ)
assert tumbler.device_type == DeviceType.TUMBLER
assert "name" not in tumbler.update_attributes
assert "name" not in tumbler.update_attributes
assert "volume_level" not in tumbler.update_attributes
assert "name" not in tumbler.device_attributes
assert "name" not in tumbler.device_attributes
assert "volume_level" not in tumbler.device_attributes

cup = ModelInfo(DeviceModel.CUP_6_OZ)
assert cup.device_type == DeviceType.CUP
assert "name" not in cup.update_attributes
assert "name" not in cup.update_attributes
assert "volume_level" not in cup.update_attributes
assert "name" not in cup.device_attributes
assert "name" not in cup.device_attributes
assert "volume_level" not in cup.device_attributes

unknown = ModelInfo()
assert unknown.model is None
assert unknown.device_type == DeviceType.MUG # fallback value
assert "name" not in unknown.update_attributes
assert "volume_level" not in unknown.update_attributes
assert "battery_voltage" not in unknown.update_attributes
assert "name" not in unknown.device_attributes
assert "volume_level" not in unknown.device_attributes
assert "battery_voltage" not in unknown.device_attributes

0 comments on commit 0cce46c

Please sign in to comment.