1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add suggested_unit_of_measurement attribute to sensors (#80638)

* Add suggested_unit_of_measurement attribute to sensors

* Lazy calculation of initial entity options

* Add type alias for entity options

* Small tweak

* Add tests

* Store suggested_unit_of_measurement in its own option key

* Adapt to renaming of IMPERIAL_SYSTEM

* Fix rebase mistakes

* Apply suggestions from code review

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
This commit is contained in:
Erik Montnemery
2022-10-24 16:08:02 +02:00
committed by GitHub
parent 0c79a9a33d
commit 6979cd95b0
9 changed files with 380 additions and 48 deletions

View File

@@ -94,6 +94,9 @@ class RegistryEntryHider(StrEnum):
USER = "user"
EntityOptionsType = Mapping[str, Mapping[str, Any]]
@attr.s(slots=True, frozen=True)
class RegistryEntry:
"""Entity Registry Entry."""
@@ -114,7 +117,7 @@ class RegistryEntry:
id: str = attr.ib(factory=uuid_util.random_uuid_hex)
has_entity_name: bool = attr.ib(default=False)
name: str | None = attr.ib(default=None)
options: Mapping[str, Mapping[str, Any]] = attr.ib(
options: EntityOptionsType = attr.ib(
default=None, converter=attr.converters.default_if_none(factory=dict) # type: ignore[misc]
)
# As set by integration
@@ -397,6 +400,8 @@ class EntityRegistry:
# To disable or hide an entity if it gets created
disabled_by: RegistryEntryDisabler | None = None,
hidden_by: RegistryEntryHider | None = None,
# Function to generate initial entity options if it gets created
get_initial_options: Callable[[], EntityOptionsType | None] | None = None,
# Data that we want entry to have
capabilities: Mapping[str, Any] | None | UndefinedType = UNDEFINED,
config_entry: ConfigEntry | None | UndefinedType = UNDEFINED,
@@ -465,6 +470,8 @@ class EntityRegistry:
"""Return None if value is UNDEFINED, otherwise return value."""
return None if value is UNDEFINED else value
initial_options = get_initial_options() if get_initial_options else None
entry = RegistryEntry(
capabilities=none_if_undefined(capabilities),
config_entry_id=none_if_undefined(config_entry_id),
@@ -474,6 +481,7 @@ class EntityRegistry:
entity_id=entity_id,
hidden_by=hidden_by,
has_entity_name=none_if_undefined(has_entity_name) or False,
options=initial_options,
original_device_class=none_if_undefined(original_device_class),
original_icon=none_if_undefined(original_icon),
original_name=none_if_undefined(original_name),
@@ -590,7 +598,7 @@ class EntityRegistry:
supported_features: int | UndefinedType = UNDEFINED,
unit_of_measurement: str | None | UndefinedType = UNDEFINED,
platform: str | None | UndefinedType = UNDEFINED,
options: Mapping[str, Mapping[str, Any]] | UndefinedType = UNDEFINED,
options: EntityOptionsType | UndefinedType = UNDEFINED,
) -> RegistryEntry:
"""Private facing update properties method."""
old = self.entities[entity_id]
@@ -779,7 +787,7 @@ class EntityRegistry:
) -> RegistryEntry:
"""Update entity options."""
old = self.entities[entity_id]
new_options: Mapping[str, Mapping[str, Any]] = {**old.options, domain: options}
new_options: EntityOptionsType = {**old.options, domain: options}
return self._async_update_entity(entity_id, options=new_options)
async def async_load(self) -> None: