Change naming of MQTT entities to correspond with HA guidelines (#95159)

* Set has_entity_name if device_name is set

* revert unneeded formatting change

* Add image platform

* Follow up comment

* Don't set `has_entity_name` without device name

* Only set has_entity_name if a valid name is set

* Follow device_class name and add tests

* Follow up comments add extra tests

* Move to helper - Log a warning

* fix test

* Allow to assign None as name explictly

* Refactor

* Log info messages when device name is not set

* Revert scene schema change - no device link

* Always set has_entity_name with device mapping

* Always set `_attr_has_entity_name`

* Cleanup
This commit is contained in:
Jan Bouwhuis 2023-07-21 12:52:10 +02:00 committed by GitHub
parent 747f4d4a73
commit 447fbf58c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 433 additions and 66 deletions

View file

@ -1117,6 +1117,45 @@ async def help_test_entity_device_info_update(
assert device.name == "Milk"
async def help_test_entity_name(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
domain: str,
config: ConfigType,
expected_friendly_name: str | None = None,
device_class: str | None = None,
) -> None:
"""Test device name setup with and without a device_class set.
This is a test helper for the _setup_common_attributes_from_config mixin.
"""
await mqtt_mock_entry()
# Add device settings to config
config = copy.deepcopy(config[mqtt.DOMAIN][domain])
config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID)
config["unique_id"] = "veryunique"
expected_entity_name = "test"
if device_class is not None:
config["device_class"] = device_class
# Do not set a name
config.pop("name")
expected_entity_name = device_class
registry = dr.async_get(hass)
data = json.dumps(config)
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data)
await hass.async_block_till_done()
device = registry.async_get_device({("mqtt", "helloworld")})
assert device is not None
entity_id = f"{domain}.beer_{expected_entity_name}"
state = hass.states.get(entity_id)
assert state is not None
assert state.name == f"Beer {expected_friendly_name}"
async def help_test_entity_id_update_subscriptions(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
@ -1390,7 +1429,7 @@ async def help_test_entity_debug_info_message(
with patch("homeassistant.util.dt.utcnow") as dt_utcnow:
dt_utcnow.return_value = start_dt
if service:
service_data = {ATTR_ENTITY_ID: f"{domain}.test"}
service_data = {ATTR_ENTITY_ID: f"{domain}.beer_test"}
if service_parameters:
service_data.update(service_parameters)
@ -1458,7 +1497,7 @@ async def help_test_entity_debug_info_remove(
"subscriptions"
]
assert len(debug_info_data["triggers"]) == 0
assert debug_info_data["entities"][0]["entity_id"] == f"{domain}.test"
assert debug_info_data["entities"][0]["entity_id"] == f"{domain}.beer_test"
entity_id = debug_info_data["entities"][0]["entity_id"]
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", "")
@ -1503,7 +1542,7 @@ async def help_test_entity_debug_info_update_entity_id(
== f"homeassistant/{domain}/bla/config"
)
assert debug_info_data["entities"][0]["discovery_data"]["payload"] == config
assert debug_info_data["entities"][0]["entity_id"] == f"{domain}.test"
assert debug_info_data["entities"][0]["entity_id"] == f"{domain}.beer_test"
assert len(debug_info_data["entities"][0]["subscriptions"]) == 1
assert {"topic": "test-topic", "messages": []} in debug_info_data["entities"][0][
"subscriptions"
@ -1511,7 +1550,7 @@ async def help_test_entity_debug_info_update_entity_id(
assert len(debug_info_data["triggers"]) == 0
entity_registry.async_update_entity(
f"{domain}.test", new_entity_id=f"{domain}.milk"
f"{domain}.beer_test", new_entity_id=f"{domain}.milk"
)
await hass.async_block_till_done()
await hass.async_block_till_done()
@ -1529,7 +1568,7 @@ async def help_test_entity_debug_info_update_entity_id(
"subscriptions"
]
assert len(debug_info_data["triggers"]) == 0
assert f"{domain}.test" not in hass.data["mqtt"].debug_info_entities
assert f"{domain}.beer_test" not in hass.data["mqtt"].debug_info_entities
async def help_test_entity_disabled_by_default(