|
8 | 8 | from homeassistant import config |
9 | 9 | from homeassistant.components import labs |
10 | 10 | from homeassistant.components.template import DOMAIN |
| 11 | +from homeassistant.config_entries import ConfigEntryState |
11 | 12 | from homeassistant.const import SERVICE_RELOAD |
12 | 13 | from homeassistant.core import Context, HomeAssistant |
13 | 14 | from homeassistant.helpers import ( |
@@ -754,3 +755,80 @@ async def test_config_entry_reload_when_labs_flag_changes( |
754 | 755 |
|
755 | 756 | assert hass.states.get("sensor.hello") is not None |
756 | 757 | assert hass.states.get("sensor.hello").state == set_state |
| 758 | + |
| 759 | + |
| 760 | +async def test_migration_1_1( |
| 761 | + hass: HomeAssistant, |
| 762 | + device_registry: dr.DeviceRegistry, |
| 763 | + entity_registry: er.EntityRegistry, |
| 764 | +) -> None: |
| 765 | + """Test migration from v1.1 removes template config entry from device.""" |
| 766 | + |
| 767 | + device_config_entry = MockConfigEntry() |
| 768 | + device_config_entry.add_to_hass(hass) |
| 769 | + device_entry = device_registry.async_get_or_create( |
| 770 | + config_entry_id=device_config_entry.entry_id, |
| 771 | + identifiers={("test", "identifier_test")}, |
| 772 | + connections={("mac", "30:31:32:33:34:35")}, |
| 773 | + ) |
| 774 | + |
| 775 | + template_config_entry = MockConfigEntry( |
| 776 | + data={}, |
| 777 | + domain=DOMAIN, |
| 778 | + options={ |
| 779 | + "name": "My template", |
| 780 | + "template_type": "sensor", |
| 781 | + "state": "{{ 'foo' }}", |
| 782 | + "device_id": device_entry.id, |
| 783 | + }, |
| 784 | + title="My template", |
| 785 | + version=1, |
| 786 | + minor_version=1, |
| 787 | + ) |
| 788 | + template_config_entry.add_to_hass(hass) |
| 789 | + |
| 790 | + # Add the helper config entry to the device |
| 791 | + device_registry.async_update_device( |
| 792 | + device_entry.id, add_config_entry_id=template_config_entry.entry_id |
| 793 | + ) |
| 794 | + |
| 795 | + # Check preconditions |
| 796 | + device_entry = device_registry.async_get(device_entry.id) |
| 797 | + assert template_config_entry.entry_id in device_entry.config_entries |
| 798 | + |
| 799 | + await hass.config_entries.async_setup(template_config_entry.entry_id) |
| 800 | + await hass.async_block_till_done() |
| 801 | + |
| 802 | + assert template_config_entry.state is ConfigEntryState.LOADED |
| 803 | + |
| 804 | + # Check that the helper config entry is removed from the device and the helper |
| 805 | + # entity is linked to the source device |
| 806 | + device_entry = device_registry.async_get(device_entry.id) |
| 807 | + assert template_config_entry.entry_id not in device_entry.config_entries |
| 808 | + template_entity_entry = entity_registry.async_get("sensor.my_template") |
| 809 | + assert template_entity_entry.device_id == device_entry.id |
| 810 | + |
| 811 | + assert template_config_entry.version == 1 |
| 812 | + assert template_config_entry.minor_version == 2 |
| 813 | + |
| 814 | + |
| 815 | +async def test_migration_from_future_version( |
| 816 | + hass: HomeAssistant, |
| 817 | +) -> None: |
| 818 | + """Test migration from future version.""" |
| 819 | + config_entry = MockConfigEntry( |
| 820 | + data={}, |
| 821 | + domain=DOMAIN, |
| 822 | + options={ |
| 823 | + "name": "hello", |
| 824 | + "template_type": "sensor", |
| 825 | + "state": "{{ 'foo' }}", |
| 826 | + }, |
| 827 | + title="My template", |
| 828 | + version=2, |
| 829 | + minor_version=1, |
| 830 | + ) |
| 831 | + config_entry.add_to_hass(hass) |
| 832 | + await hass.config_entries.async_setup(config_entry.entry_id) |
| 833 | + await hass.async_block_till_done() |
| 834 | + assert config_entry.state is ConfigEntryState.MIGRATION_ERROR |
0 commit comments