|
10 | 10 | BinarySensorState,
|
11 | 11 | SensorInfo,
|
12 | 12 | SensorState,
|
| 13 | + build_unique_id, |
13 | 14 | )
|
| 15 | +import pytest |
14 | 16 |
|
| 17 | +from homeassistant.components.esphome import DOMAIN |
15 | 18 | from homeassistant.const import (
|
16 | 19 | ATTR_FRIENDLY_NAME,
|
17 | 20 | ATTR_RESTORED,
|
18 | 21 | EVENT_HOMEASSISTANT_STOP,
|
19 | 22 | STATE_OFF,
|
20 | 23 | STATE_ON,
|
21 | 24 | STATE_UNAVAILABLE,
|
| 25 | + Platform, |
22 | 26 | )
|
23 | 27 | from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
|
24 | 28 | from homeassistant.helpers import entity_registry as er
|
@@ -513,3 +517,151 @@ async def test_entity_without_name_device_with_friendly_name(
|
513 | 517 | # Make sure we have set the name to `None` as otherwise
|
514 | 518 | # the friendly_name will be "The Best Mixer "
|
515 | 519 | assert state.attributes[ATTR_FRIENDLY_NAME] == "The Best Mixer"
|
| 520 | + |
| 521 | + |
| 522 | +@pytest.mark.usefixtures("hass_storage") |
| 523 | +async def test_entity_id_preserved_on_upgrade( |
| 524 | + hass: HomeAssistant, |
| 525 | + mock_client: APIClient, |
| 526 | + mock_esphome_device: MockESPHomeDeviceType, |
| 527 | + entity_registry: er.EntityRegistry, |
| 528 | +) -> None: |
| 529 | + """Test entity_id is preserved on upgrade.""" |
| 530 | + entity_info = [ |
| 531 | + BinarySensorInfo( |
| 532 | + object_id="my", |
| 533 | + key=1, |
| 534 | + name="my", |
| 535 | + unique_id="binary_sensor_my", |
| 536 | + ), |
| 537 | + ] |
| 538 | + states = [ |
| 539 | + BinarySensorState(key=1, state=True, missing_state=False), |
| 540 | + ] |
| 541 | + user_service = [] |
| 542 | + assert ( |
| 543 | + build_unique_id("11:22:33:44:55:AA", entity_info[0]) |
| 544 | + == "11:22:33:44:55:AA-binary_sensor-my" |
| 545 | + ) |
| 546 | + |
| 547 | + entry = entity_registry.async_get_or_create( |
| 548 | + Platform.BINARY_SENSOR, |
| 549 | + DOMAIN, |
| 550 | + "11:22:33:44:55:AA-binary_sensor-my", |
| 551 | + suggested_object_id="should_not_change", |
| 552 | + ) |
| 553 | + assert entry.entity_id == "binary_sensor.should_not_change" |
| 554 | + await mock_esphome_device( |
| 555 | + mock_client=mock_client, |
| 556 | + entity_info=entity_info, |
| 557 | + user_service=user_service, |
| 558 | + states=states, |
| 559 | + device_info={"friendly_name": "The Best Mixer", "name": "mixer"}, |
| 560 | + ) |
| 561 | + state = hass.states.get("binary_sensor.should_not_change") |
| 562 | + assert state is not None |
| 563 | + |
| 564 | + |
| 565 | +@pytest.mark.usefixtures("hass_storage") |
| 566 | +async def test_entity_id_preserved_on_upgrade_old_format_entity_id( |
| 567 | + hass: HomeAssistant, |
| 568 | + mock_client: APIClient, |
| 569 | + mock_esphome_device: MockESPHomeDeviceType, |
| 570 | + entity_registry: er.EntityRegistry, |
| 571 | +) -> None: |
| 572 | + """Test entity_id is preserved on upgrade from old format.""" |
| 573 | + entity_info = [ |
| 574 | + BinarySensorInfo( |
| 575 | + object_id="my", |
| 576 | + key=1, |
| 577 | + name="my", |
| 578 | + unique_id="binary_sensor_my", |
| 579 | + ), |
| 580 | + ] |
| 581 | + states = [ |
| 582 | + BinarySensorState(key=1, state=True, missing_state=False), |
| 583 | + ] |
| 584 | + user_service = [] |
| 585 | + assert ( |
| 586 | + build_unique_id("11:22:33:44:55:AA", entity_info[0]) |
| 587 | + == "11:22:33:44:55:AA-binary_sensor-my" |
| 588 | + ) |
| 589 | + |
| 590 | + entry = entity_registry.async_get_or_create( |
| 591 | + Platform.BINARY_SENSOR, |
| 592 | + DOMAIN, |
| 593 | + "11:22:33:44:55:AA-binary_sensor-my", |
| 594 | + suggested_object_id="my", |
| 595 | + ) |
| 596 | + assert entry.entity_id == "binary_sensor.my" |
| 597 | + await mock_esphome_device( |
| 598 | + mock_client=mock_client, |
| 599 | + entity_info=entity_info, |
| 600 | + user_service=user_service, |
| 601 | + states=states, |
| 602 | + device_info={"name": "mixer"}, |
| 603 | + ) |
| 604 | + state = hass.states.get("binary_sensor.my") |
| 605 | + assert state is not None |
| 606 | + |
| 607 | + |
| 608 | +async def test_entity_id_preserved_on_upgrade_when_in_storage( |
| 609 | + hass: HomeAssistant, |
| 610 | + mock_client: APIClient, |
| 611 | + hass_storage: dict[str, Any], |
| 612 | + mock_esphome_device: MockESPHomeDeviceType, |
| 613 | + entity_registry: er.EntityRegistry, |
| 614 | +) -> None: |
| 615 | + """Test entity_id is preserved on upgrade with user defined entity_id.""" |
| 616 | + entity_info = [ |
| 617 | + BinarySensorInfo( |
| 618 | + object_id="my", |
| 619 | + key=1, |
| 620 | + name="my", |
| 621 | + unique_id="binary_sensor_my", |
| 622 | + ), |
| 623 | + ] |
| 624 | + states = [ |
| 625 | + BinarySensorState(key=1, state=True, missing_state=False), |
| 626 | + ] |
| 627 | + user_service = [] |
| 628 | + device = await mock_esphome_device( |
| 629 | + mock_client=mock_client, |
| 630 | + entity_info=entity_info, |
| 631 | + user_service=user_service, |
| 632 | + states=states, |
| 633 | + device_info={"friendly_name": "The Best Mixer", "name": "mixer"}, |
| 634 | + ) |
| 635 | + state = hass.states.get("binary_sensor.mixer_my") |
| 636 | + assert state is not None |
| 637 | + # now rename the entity |
| 638 | + ent_reg_entry = entity_registry.async_get_or_create( |
| 639 | + Platform.BINARY_SENSOR, |
| 640 | + DOMAIN, |
| 641 | + "11:22:33:44:55:AA-binary_sensor-my", |
| 642 | + ) |
| 643 | + entity_registry.async_update_entity( |
| 644 | + ent_reg_entry.entity_id, |
| 645 | + new_entity_id="binary_sensor.user_named", |
| 646 | + ) |
| 647 | + await hass.config_entries.async_unload(device.entry.entry_id) |
| 648 | + await hass.async_block_till_done() |
| 649 | + entry = device.entry |
| 650 | + entry_id = entry.entry_id |
| 651 | + storage_key = f"esphome.{entry_id}" |
| 652 | + assert len(hass_storage[storage_key]["data"]["binary_sensor"]) == 1 |
| 653 | + binary_sensor_data: dict[str, Any] = hass_storage[storage_key]["data"][ |
| 654 | + "binary_sensor" |
| 655 | + ][0] |
| 656 | + assert binary_sensor_data["name"] == "my" |
| 657 | + assert binary_sensor_data["object_id"] == "my" |
| 658 | + device = await mock_esphome_device( |
| 659 | + mock_client=mock_client, |
| 660 | + entity_info=entity_info, |
| 661 | + user_service=user_service, |
| 662 | + states=states, |
| 663 | + entry=entry, |
| 664 | + device_info={"friendly_name": "The Best Mixer", "name": "mixer"}, |
| 665 | + ) |
| 666 | + state = hass.states.get("binary_sensor.user_named") |
| 667 | + assert state is not None |
0 commit comments