|
4 | 4 | from unittest.mock import Mock, patch |
5 | 5 |
|
6 | 6 | from aiohue.discovery import URL_NUPNP |
7 | | -from aiohue.errors import LinkButtonNotPressed |
| 7 | +from aiohue.errors import AiohueException, LinkButtonNotPressed |
8 | 8 | import pytest |
9 | 9 | import voluptuous as vol |
10 | 10 |
|
@@ -732,3 +732,216 @@ async def test_bridge_connection_failed( |
732 | 732 | ) |
733 | 733 | assert result["type"] is FlowResultType.ABORT |
734 | 734 | assert result["reason"] == "cannot_connect" |
| 735 | + |
| 736 | + |
| 737 | +async def test_bsb003_bridge_discovery( |
| 738 | + hass: HomeAssistant, |
| 739 | + aioclient_mock: AiohttpClientMocker, |
| 740 | + device_registry: dr.DeviceRegistry, |
| 741 | +) -> None: |
| 742 | + """Test a bridge being discovered.""" |
| 743 | + entry = MockConfigEntry( |
| 744 | + domain=const.DOMAIN, |
| 745 | + data={"host": "192.168.1.217", "api_version": 2, "api_key": "abc"}, |
| 746 | + unique_id="bsb002_00000", |
| 747 | + ) |
| 748 | + entry.add_to_hass(hass) |
| 749 | + device = device_registry.async_get_or_create( |
| 750 | + config_entry_id=entry.entry_id, |
| 751 | + identifiers={(const.DOMAIN, "bsb002_00000")}, |
| 752 | + connections={(dr.CONNECTION_NETWORK_MAC, "AA:BB:CC:DD:EE:FF")}, |
| 753 | + ) |
| 754 | + create_mock_api_discovery( |
| 755 | + aioclient_mock, |
| 756 | + [("192.168.1.217", "bsb002_00000"), ("192.168.1.218", "bsb003_00000")], |
| 757 | + ) |
| 758 | + disc_bridge = get_discovered_bridge( |
| 759 | + bridge_id="bsb003_00000", host="192.168.1.218", supports_v2=True |
| 760 | + ) |
| 761 | + |
| 762 | + with ( |
| 763 | + patch( |
| 764 | + "homeassistant.components.hue.config_flow.discover_bridge", |
| 765 | + return_value=disc_bridge, |
| 766 | + ), |
| 767 | + patch( |
| 768 | + "homeassistant.components.hue.config_flow.HueBridgeV2", |
| 769 | + autospec=True, |
| 770 | + ) as mock_bridge, |
| 771 | + ): |
| 772 | + mock_bridge.return_value.fetch_full_state.return_value = {} |
| 773 | + result = await hass.config_entries.flow.async_init( |
| 774 | + const.DOMAIN, |
| 775 | + context={"source": config_entries.SOURCE_ZEROCONF}, |
| 776 | + data=ZeroconfServiceInfo( |
| 777 | + ip_address=ip_address("192.168.1.218"), |
| 778 | + ip_addresses=[ip_address("192.168.1.218")], |
| 779 | + port=443, |
| 780 | + hostname="Philips-hue.local", |
| 781 | + type="_hue._tcp.local.", |
| 782 | + name="Philips Hue - ABCABC._hue._tcp.local.", |
| 783 | + properties={ |
| 784 | + "bridgeid": "bsb003_00000", |
| 785 | + "modelid": "BSB003", |
| 786 | + }, |
| 787 | + ), |
| 788 | + ) |
| 789 | + |
| 790 | + assert result["type"] is FlowResultType.ABORT |
| 791 | + assert result["reason"] == "migrated_bridge" |
| 792 | + |
| 793 | + migrated_device = device_registry.async_get(device.id) |
| 794 | + |
| 795 | + assert migrated_device is not None |
| 796 | + assert len(migrated_device.identifiers) == 1 |
| 797 | + assert list(migrated_device.identifiers)[0] == (const.DOMAIN, "bsb003_00000") |
| 798 | + # The tests don't add new connection, but that will happen |
| 799 | + # outside of the config flow |
| 800 | + assert len(migrated_device.connections) == 0 |
| 801 | + assert entry.data["host"] == "192.168.1.218" |
| 802 | + |
| 803 | + |
| 804 | +async def test_bsb003_bridge_discovery_old_version( |
| 805 | + hass: HomeAssistant, |
| 806 | + aioclient_mock: AiohttpClientMocker, |
| 807 | + device_registry: dr.DeviceRegistry, |
| 808 | +) -> None: |
| 809 | + """Test a bridge being discovered.""" |
| 810 | + entry = MockConfigEntry( |
| 811 | + domain=const.DOMAIN, |
| 812 | + data={"host": "192.168.1.217", "api_version": 1, "api_key": "abc"}, |
| 813 | + unique_id="bsb002_00000", |
| 814 | + ) |
| 815 | + entry.add_to_hass(hass) |
| 816 | + disc_bridge = get_discovered_bridge( |
| 817 | + bridge_id="bsb003_00000", host="192.168.1.218", supports_v2=True |
| 818 | + ) |
| 819 | + |
| 820 | + with patch( |
| 821 | + "homeassistant.components.hue.config_flow.discover_bridge", |
| 822 | + return_value=disc_bridge, |
| 823 | + ): |
| 824 | + result = await hass.config_entries.flow.async_init( |
| 825 | + const.DOMAIN, |
| 826 | + context={"source": config_entries.SOURCE_ZEROCONF}, |
| 827 | + data=ZeroconfServiceInfo( |
| 828 | + ip_address=ip_address("192.168.1.218"), |
| 829 | + ip_addresses=[ip_address("192.168.1.218")], |
| 830 | + port=443, |
| 831 | + hostname="Philips-hue.local", |
| 832 | + type="_hue._tcp.local.", |
| 833 | + name="Philips Hue - ABCABC._hue._tcp.local.", |
| 834 | + properties={ |
| 835 | + "bridgeid": "bsb003_00000", |
| 836 | + "modelid": "BSB003", |
| 837 | + }, |
| 838 | + ), |
| 839 | + ) |
| 840 | + |
| 841 | + assert result["type"] is FlowResultType.FORM |
| 842 | + assert result["step_id"] == "link" |
| 843 | + |
| 844 | + |
| 845 | +async def test_bsb003_bridge_discovery_same_host( |
| 846 | + hass: HomeAssistant, |
| 847 | + aioclient_mock: AiohttpClientMocker, |
| 848 | + device_registry: dr.DeviceRegistry, |
| 849 | +) -> None: |
| 850 | + """Test a bridge being discovered.""" |
| 851 | + entry = MockConfigEntry( |
| 852 | + domain=const.DOMAIN, |
| 853 | + data={"host": "192.168.1.217", "api_version": 2, "api_key": "abc"}, |
| 854 | + unique_id="bsb002_00000", |
| 855 | + ) |
| 856 | + entry.add_to_hass(hass) |
| 857 | + create_mock_api_discovery( |
| 858 | + aioclient_mock, |
| 859 | + [("192.168.1.217", "bsb003_00000")], |
| 860 | + ) |
| 861 | + disc_bridge = get_discovered_bridge( |
| 862 | + bridge_id="bsb003_00000", host="192.168.1.217", supports_v2=True |
| 863 | + ) |
| 864 | + |
| 865 | + with ( |
| 866 | + patch( |
| 867 | + "homeassistant.components.hue.config_flow.discover_bridge", |
| 868 | + return_value=disc_bridge, |
| 869 | + ), |
| 870 | + patch( |
| 871 | + "homeassistant.components.hue.config_flow.HueBridgeV2", |
| 872 | + autospec=True, |
| 873 | + ), |
| 874 | + ): |
| 875 | + result = await hass.config_entries.flow.async_init( |
| 876 | + const.DOMAIN, |
| 877 | + context={"source": config_entries.SOURCE_ZEROCONF}, |
| 878 | + data=ZeroconfServiceInfo( |
| 879 | + ip_address=ip_address("192.168.1.217"), |
| 880 | + ip_addresses=[ip_address("192.168.1.217")], |
| 881 | + port=443, |
| 882 | + hostname="Philips-hue.local", |
| 883 | + type="_hue._tcp.local.", |
| 884 | + name="Philips Hue - ABCABC._hue._tcp.local.", |
| 885 | + properties={ |
| 886 | + "bridgeid": "bsb003_00000", |
| 887 | + "modelid": "BSB003", |
| 888 | + }, |
| 889 | + ), |
| 890 | + ) |
| 891 | + |
| 892 | + assert result["type"] is FlowResultType.FORM |
| 893 | + assert result["step_id"] == "link" |
| 894 | + |
| 895 | + |
| 896 | +@pytest.mark.parametrize("exception", [AiohueException, ClientError]) |
| 897 | +async def test_bsb003_bridge_discovery_cannot_connect( |
| 898 | + hass: HomeAssistant, |
| 899 | + aioclient_mock: AiohttpClientMocker, |
| 900 | + device_registry: dr.DeviceRegistry, |
| 901 | + exception: Exception, |
| 902 | +) -> None: |
| 903 | + """Test a bridge being discovered.""" |
| 904 | + entry = MockConfigEntry( |
| 905 | + domain=const.DOMAIN, |
| 906 | + data={"host": "192.168.1.217", "api_version": 2, "api_key": "abc"}, |
| 907 | + unique_id="bsb002_00000", |
| 908 | + ) |
| 909 | + entry.add_to_hass(hass) |
| 910 | + create_mock_api_discovery( |
| 911 | + aioclient_mock, |
| 912 | + [("192.168.1.217", "bsb003_00000")], |
| 913 | + ) |
| 914 | + disc_bridge = get_discovered_bridge( |
| 915 | + bridge_id="bsb003_00000", host="192.168.1.217", supports_v2=True |
| 916 | + ) |
| 917 | + |
| 918 | + with ( |
| 919 | + patch( |
| 920 | + "homeassistant.components.hue.config_flow.discover_bridge", |
| 921 | + return_value=disc_bridge, |
| 922 | + ), |
| 923 | + patch( |
| 924 | + "homeassistant.components.hue.config_flow.HueBridgeV2", |
| 925 | + autospec=True, |
| 926 | + ) as mock_bridge, |
| 927 | + ): |
| 928 | + mock_bridge.return_value.fetch_full_state.side_effect = exception |
| 929 | + result = await hass.config_entries.flow.async_init( |
| 930 | + const.DOMAIN, |
| 931 | + context={"source": config_entries.SOURCE_ZEROCONF}, |
| 932 | + data=ZeroconfServiceInfo( |
| 933 | + ip_address=ip_address("192.168.1.217"), |
| 934 | + ip_addresses=[ip_address("192.168.1.217")], |
| 935 | + port=443, |
| 936 | + hostname="Philips-hue.local", |
| 937 | + type="_hue._tcp.local.", |
| 938 | + name="Philips Hue - ABCABC._hue._tcp.local.", |
| 939 | + properties={ |
| 940 | + "bridgeid": "bsb003_00000", |
| 941 | + "modelid": "BSB003", |
| 942 | + }, |
| 943 | + ), |
| 944 | + ) |
| 945 | + |
| 946 | + assert result["type"] is FlowResultType.FORM |
| 947 | + assert result["step_id"] == "link" |
0 commit comments