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

Use legacy rules for ESPHome entity_id construction if friendly_name is unset #97578

Merged
merged 1 commit into from
Aug 1, 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
23 changes: 19 additions & 4 deletions homeassistant/components/esphome/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,29 @@ def __init__(
assert entry_data.device_info is not None
device_info = entry_data.device_info
self._device_info = device_info
if object_id := entity_info.object_id:
# Use the object_id to suggest the entity_id
self.entity_id = f"{domain}.{device_info.name}_{object_id}"
self._attr_device_info = DeviceInfo(
connections={(dr.CONNECTION_NETWORK_MAC, device_info.mac_address)}
)
self._entry_id = entry_data.entry_id
self._attr_has_entity_name = bool(device_info.friendly_name)
#
# If `friendly_name` is set, we use the Friendly naming rules, if
# `friendly_name` is not set we make an exception to the naming rules for
# backwards compatibility and use the Legacy naming rules.
#
# Friendly naming
# - Friendly name is prepended to entity names
# - Device Name is prepended to entity ids
# - Entity id is constructed from device name and object id
#
# Legacy naming
# - Device name is not prepended to entity names
# - Device name is not prepended to entity ids
# - Entity id is constructed from entity name
#
if not device_info.friendly_name:
return
self._attr_has_entity_name = True
self.entity_id = f"{domain}.{device_info.name}_{entity_info.object_id}"

async def async_added_to_hass(self) -> None:
"""Register callbacks."""
Expand Down
2 changes: 1 addition & 1 deletion tests/components/esphome/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,6 @@ async def test_esphome_device_without_friendly_name(
states=states,
device_info={"friendly_name": None},
)
state = hass.states.get("binary_sensor.test_mybinary_sensor")
state = hass.states.get("binary_sensor.my_binary_sensor")
assert state is not None
assert state.state == STATE_ON