diff --git a/tests/components/senseme/__init__.py b/tests/components/senseme/__init__.py deleted file mode 100644 index 43d586328b..0000000000 --- a/tests/components/senseme/__init__.py +++ /dev/null @@ -1,142 +0,0 @@ -"""Tests for the SenseME integration.""" - -from contextlib import contextmanager -from unittest.mock import AsyncMock, MagicMock, patch - -from aiosenseme import SensemeDevice, SensemeDiscovery - -from homeassistant.components.senseme import config_flow - -MOCK_NAME = "Haiku Fan" -MOCK_UUID = "77a6b7b3-925d-4695-a415-76d76dca4444" -MOCK_ADDRESS = "127.0.0.1" -MOCK_MAC = "20:F8:5E:92:5A:75" - - -def _mock_device(): - device = MagicMock(auto_spec=SensemeDevice) - device.async_update = AsyncMock() - device.model = "Haiku Fan" - device.fan_speed_max = 7 - device.mac = "aa:bb:cc:dd:ee:ff" - device.fan_dir = "REV" - device.has_light = True - device.is_light = False - device.light_brightness = 50 - device.room_name = "Main" - device.room_type = "Main" - device.fw_version = "1" - device.fan_autocomfort = "COOLING" - device.fan_smartmode = "OFF" - device.fan_whoosh_mode = "on" - device.name = MOCK_NAME - device.uuid = MOCK_UUID - device.address = MOCK_ADDRESS - device.get_device_info = { - "name": MOCK_NAME, - "uuid": MOCK_UUID, - "mac": MOCK_ADDRESS, - "address": MOCK_ADDRESS, - "base_model": "FAN,HAIKU,HSERIES", - "has_light": False, - "has_sensor": True, - "is_fan": True, - "is_light": False, - } - return device - - -device_alternate_ip = MagicMock(auto_spec=SensemeDevice) -device_alternate_ip.async_update = AsyncMock() -device_alternate_ip.model = "Haiku Fan" -device_alternate_ip.fan_speed_max = 7 -device_alternate_ip.mac = "aa:bb:cc:dd:ee:ff" -device_alternate_ip.fan_dir = "REV" -device_alternate_ip.room_name = "Main" -device_alternate_ip.room_type = "Main" -device_alternate_ip.fw_version = "1" -device_alternate_ip.fan_autocomfort = "on" -device_alternate_ip.fan_smartmode = "on" -device_alternate_ip.fan_whoosh_mode = "on" -device_alternate_ip.name = MOCK_NAME -device_alternate_ip.uuid = MOCK_UUID -device_alternate_ip.address = "127.0.0.8" -device_alternate_ip.get_device_info = { - "name": MOCK_NAME, - "uuid": MOCK_UUID, - "mac": "20:F8:5E:92:5A:75", - "address": "127.0.0.8", - "base_model": "FAN,HAIKU,HSERIES", - "has_light": False, - "has_sensor": True, - "is_fan": True, - "is_light": False, -} - - -device2 = MagicMock(auto_spec=SensemeDevice) -device2.async_update = AsyncMock() -device2.model = "Haiku Fan" -device2.fan_speed_max = 7 -device2.mac = "aa:bb:cc:dd:ee:ff" -device2.fan_dir = "FWD" -device2.room_name = "Main" -device2.room_type = "Main" -device2.fw_version = "1" -device2.fan_autocomfort = "on" -device2.fan_smartmode = "on" -device2.fan_whoosh_mode = "on" -device2.name = "Device 2" -device2.uuid = "uuid2" -device2.address = "127.0.0.2" -device2.get_device_info = { - "name": "Device 2", - "uuid": "uuid2", - "mac": "20:F8:5E:92:5A:76", - "address": "127.0.0.2", - "base_model": "FAN,HAIKU,HSERIES", - "has_light": True, - "has_sensor": True, - "is_fan": True, - "is_light": False, -} - -device_no_uuid = MagicMock(auto_spec=SensemeDevice) -device_no_uuid.uuid = None - - -MOCK_DEVICE = _mock_device() -MOCK_DEVICE_ALTERNATE_IP = device_alternate_ip -MOCK_DEVICE2 = device2 -MOCK_DEVICE_NO_UUID = device_no_uuid - - -def _patch_discovery(device=None, no_device=None): - """Patch discovery.""" - mock_senseme_discovery = MagicMock(auto_spec=SensemeDiscovery) - if not no_device: - mock_senseme_discovery.devices = [device or MOCK_DEVICE] - - @contextmanager - def _patcher(): - with patch.object(config_flow, "DISCOVER_TIMEOUT", 0), patch( - "homeassistant.components.senseme.discovery.SensemeDiscovery", - return_value=mock_senseme_discovery, - ): - yield - - return _patcher() - - -def _patch_device(device=None, no_device=False): - async def _device_mocker(*args, **kwargs): - if no_device: - return False, None - if device: - return True, device - return True, _mock_device() - - return patch( - "homeassistant.components.senseme.async_get_device_by_device_info", - new=_device_mocker, - ) diff --git a/tests/components/senseme/test_config_flow.py b/tests/components/senseme/test_config_flow.py deleted file mode 100644 index 63850dff3b..0000000000 --- a/tests/components/senseme/test_config_flow.py +++ /dev/null @@ -1,361 +0,0 @@ -"""Test the SenseME config flow.""" -from unittest.mock import patch - -from homeassistant import config_entries -from homeassistant.components import dhcp -from homeassistant.components.senseme.const import DOMAIN -from homeassistant.const import CONF_HOST, CONF_ID -from homeassistant.core import HomeAssistant -from homeassistant.data_entry_flow import FlowResultType - -from . import ( - MOCK_ADDRESS, - MOCK_DEVICE, - MOCK_DEVICE2, - MOCK_DEVICE_ALTERNATE_IP, - MOCK_DEVICE_NO_UUID, - MOCK_MAC, - MOCK_UUID, - _patch_discovery, -) - -from tests.common import MockConfigEntry - -DHCP_DISCOVERY = dhcp.DhcpServiceInfo( - hostname="any", - ip=MOCK_ADDRESS, - macaddress=MOCK_MAC, -) - - -async def test_form_user(hass: HomeAssistant) -> None: - """Test we get the form as a user.""" - - with _patch_discovery(), patch( - "homeassistant.components.senseme.async_setup_entry", - return_value=True, - ) as mock_setup_entry: - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - assert result["type"] == FlowResultType.FORM - assert not result["errors"] - - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - "device": MOCK_UUID, - }, - ) - await hass.async_block_till_done() - - assert result2["type"] == FlowResultType.CREATE_ENTRY - assert result2["title"] == "Haiku Fan" - assert result2["data"] == { - "info": MOCK_DEVICE.get_device_info, - } - assert len(mock_setup_entry.mock_calls) == 1 - - -async def test_form_user_manual_entry(hass: HomeAssistant) -> None: - """Test we get the form as a user with a discovery but user chooses manual.""" - - with _patch_discovery(): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - assert result["type"] == FlowResultType.FORM - assert not result["errors"] - - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - "device": None, - }, - ) - await hass.async_block_till_done() - - assert result2["type"] == FlowResultType.FORM - assert result2["step_id"] == "manual" - - with patch( - "homeassistant.components.senseme.config_flow.async_get_device_by_ip_address", - return_value=MOCK_DEVICE, - ), patch( - "homeassistant.components.senseme.async_setup_entry", - return_value=True, - ) as mock_setup_entry: - result3 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - CONF_HOST: MOCK_ADDRESS, - }, - ) - await hass.async_block_till_done() - - assert result3["type"] == FlowResultType.CREATE_ENTRY - assert result3["title"] == "Haiku Fan" - assert result3["data"] == { - "info": MOCK_DEVICE.get_device_info, - } - assert len(mock_setup_entry.mock_calls) == 1 - - -async def test_form_user_no_discovery(hass: HomeAssistant) -> None: - """Test we get the form as a user with no discovery.""" - - with _patch_discovery(no_device=True), patch( - "homeassistant.components.senseme.config_flow.async_get_device_by_ip_address", - return_value=MOCK_DEVICE, - ), patch( - "homeassistant.components.senseme.async_setup_entry", - return_value=True, - ) as mock_setup_entry: - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - assert result["type"] == FlowResultType.FORM - assert not result["errors"] - - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - CONF_HOST: "not a valid address", - }, - ) - await hass.async_block_till_done() - - assert result2["type"] == FlowResultType.FORM - assert result2["step_id"] == "manual" - assert result2["errors"] == {CONF_HOST: "invalid_host"} - - result3 = await hass.config_entries.flow.async_configure( - result2["flow_id"], - { - CONF_HOST: MOCK_ADDRESS, - }, - ) - await hass.async_block_till_done() - - assert result3["type"] == FlowResultType.CREATE_ENTRY - assert result3["title"] == "Haiku Fan" - assert result3["data"] == { - "info": MOCK_DEVICE.get_device_info, - } - assert len(mock_setup_entry.mock_calls) == 1 - - -async def test_form_user_manual_entry_cannot_connect(hass: HomeAssistant) -> None: - """Test we get the form as a user.""" - - with _patch_discovery(): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - assert result["type"] == FlowResultType.FORM - assert not result["errors"] - - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - "device": None, - }, - ) - await hass.async_block_till_done() - - assert result2["type"] == FlowResultType.FORM - assert result2["step_id"] == "manual" - - with patch( - "homeassistant.components.senseme.config_flow.async_get_device_by_ip_address", - return_value=None, - ): - result3 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - CONF_HOST: MOCK_ADDRESS, - }, - ) - await hass.async_block_till_done() - - assert result3["type"] == FlowResultType.FORM - assert result3["step_id"] == "manual" - assert result3["errors"] == {CONF_HOST: "cannot_connect"} - - -async def test_discovery(hass: HomeAssistant) -> None: - """Test we can setup a discovered device.""" - entry = MockConfigEntry( - domain=DOMAIN, - data={ - "info": MOCK_DEVICE2.get_device_info, - }, - unique_id=MOCK_DEVICE2.uuid, - ) - entry.add_to_hass(hass) - - with _patch_discovery(), patch( - "homeassistant.components.senseme.async_get_device_by_device_info", - return_value=(True, MOCK_DEVICE2), - ): - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - with _patch_discovery(), patch( - "homeassistant.components.senseme.async_setup_entry", - return_value=True, - ) as mock_setup_entry: - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={CONF_ID: MOCK_UUID}, - ) - assert result["type"] == FlowResultType.FORM - assert not result["errors"] - - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - "device": MOCK_UUID, - }, - ) - await hass.async_block_till_done() - - assert result2["type"] == FlowResultType.CREATE_ENTRY - assert result2["title"] == "Haiku Fan" - assert result2["data"] == { - "info": MOCK_DEVICE.get_device_info, - } - assert len(mock_setup_entry.mock_calls) == 1 - - -async def test_discovery_existing_device_no_ip_change(hass: HomeAssistant) -> None: - """Test we can setup a discovered device.""" - entry = MockConfigEntry( - domain=DOMAIN, - data={ - "info": MOCK_DEVICE.get_device_info, - }, - unique_id=MOCK_DEVICE.uuid, - ) - entry.add_to_hass(hass) - - with _patch_discovery(), patch( - "homeassistant.components.senseme.async_get_device_by_device_info", - return_value=(True, MOCK_DEVICE), - ): - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - with _patch_discovery(): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={CONF_ID: MOCK_UUID}, - ) - assert result["type"] == FlowResultType.ABORT - assert result["reason"] == "already_configured" - - -async def test_discovery_existing_device_ip_change(hass: HomeAssistant) -> None: - """Test a config entry ips get updated from discovery.""" - entry = MockConfigEntry( - domain=DOMAIN, - data={ - "info": MOCK_DEVICE.get_device_info, - }, - unique_id=MOCK_DEVICE.uuid, - ) - entry.add_to_hass(hass) - - with _patch_discovery(device=MOCK_DEVICE_ALTERNATE_IP), patch( - "homeassistant.components.senseme.async_get_device_by_device_info", - return_value=(True, MOCK_DEVICE), - ): - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, - data={CONF_ID: MOCK_UUID}, - ) - await hass.async_block_till_done() - - assert result["type"] == FlowResultType.ABORT - assert result["reason"] == "already_configured" - assert entry.data["info"]["address"] == "127.0.0.8" - - -async def test_dhcp_discovery_existing_config_entry(hass: HomeAssistant) -> None: - """Test dhcp discovery is aborted if there is an existing config entry.""" - entry = MockConfigEntry( - domain=DOMAIN, - data={ - "info": MOCK_DEVICE2.get_device_info, - }, - unique_id=MOCK_DEVICE2.uuid, - ) - entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=DHCP_DISCOVERY - ) - assert result["type"] == FlowResultType.ABORT - assert result["reason"] == "already_configured" - - -async def test_dhcp_discovery(hass: HomeAssistant) -> None: - """Test we can setup a dhcp discovered device.""" - with _patch_discovery(), patch( - "homeassistant.components.senseme.config_flow.async_get_device_by_ip_address", - return_value=MOCK_DEVICE, - ), patch( - "homeassistant.components.senseme.async_setup_entry", - return_value=True, - ) as mock_setup_entry: - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=DHCP_DISCOVERY - ) - assert result["type"] == FlowResultType.FORM - assert not result["errors"] - - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - { - "device": MOCK_UUID, - }, - ) - await hass.async_block_till_done() - - assert result2["type"] == FlowResultType.CREATE_ENTRY - assert result2["title"] == "Haiku Fan" - assert result2["data"] == { - "info": MOCK_DEVICE.get_device_info, - } - assert len(mock_setup_entry.mock_calls) == 1 - - -async def test_dhcp_discovery_cannot_connect(hass: HomeAssistant) -> None: - """Test we abort if we cannot cannot to a dhcp discovered device.""" - with _patch_discovery(), patch( - "homeassistant.components.senseme.config_flow.async_get_device_by_ip_address", - return_value=None, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=DHCP_DISCOVERY - ) - assert result["type"] == FlowResultType.ABORT - assert result["reason"] == "cannot_connect" - - -async def test_dhcp_discovery_cannot_connect_no_uuid(hass: HomeAssistant) -> None: - """Test we abort if the discovered device has no uuid.""" - with _patch_discovery(), patch( - "homeassistant.components.senseme.config_flow.async_get_device_by_ip_address", - return_value=MOCK_DEVICE_NO_UUID, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=DHCP_DISCOVERY - ) - assert result["type"] == FlowResultType.ABORT - assert result["reason"] == "cannot_connect" diff --git a/tests/components/senseme/test_light.py b/tests/components/senseme/test_light.py deleted file mode 100644 index d4a8eddc91..0000000000 --- a/tests/components/senseme/test_light.py +++ /dev/null @@ -1,115 +0,0 @@ -"""Tests for senseme light platform.""" -from aiosenseme import SensemeDevice - -from homeassistant.components import senseme -from homeassistant.components.light import ( - ATTR_BRIGHTNESS, - ATTR_COLOR_MODE, - ATTR_COLOR_TEMP, - ATTR_SUPPORTED_COLOR_MODES, - DOMAIN as LIGHT_DOMAIN, - SERVICE_TURN_OFF, - SERVICE_TURN_ON, - ColorMode, -) -from homeassistant.components.senseme.const import DOMAIN -from homeassistant.const import ATTR_ENTITY_ID, STATE_ON -from homeassistant.core import HomeAssistant -from homeassistant.helpers import entity_registry as er -from homeassistant.setup import async_setup_component - -from . import _mock_device, _patch_device, _patch_discovery - -from tests.common import MockConfigEntry - - -async def _setup_mocked_entry(hass: HomeAssistant, device: SensemeDevice) -> None: - """Set up a mocked entry.""" - entry = MockConfigEntry( - domain=DOMAIN, - data={"info": device.get_device_info}, - unique_id=device.uuid, - ) - entry.add_to_hass(hass) - with _patch_discovery(), _patch_device(device=device): - await async_setup_component(hass, senseme.DOMAIN, {senseme.DOMAIN: {}}) - await hass.async_block_till_done() - - -async def test_light_unique_id(hass: HomeAssistant) -> None: - """Test a light unique id.""" - device = _mock_device() - await _setup_mocked_entry(hass, device) - entity_id = "light.haiku_fan" - entity_registry = er.async_get(hass) - assert entity_registry.async_get(entity_id).unique_id == f"{device.uuid}-LIGHT" - state = hass.states.get(entity_id) - assert state.state == STATE_ON - - -async def test_fan_light(hass: HomeAssistant) -> None: - """Test a fan light.""" - device = _mock_device() - await _setup_mocked_entry(hass, device) - entity_id = "light.haiku_fan" - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - attributes = state.attributes - assert attributes[ATTR_BRIGHTNESS] == 255 - assert attributes[ATTR_COLOR_MODE] == ColorMode.BRIGHTNESS - assert attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.BRIGHTNESS] - - await hass.services.async_call( - LIGHT_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True - ) - assert device.light_on is False - - await hass.services.async_call( - LIGHT_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True - ) - assert device.light_on is True - - -async def test_fan_light_no_brightness(hass: HomeAssistant) -> None: - """Test a fan light without brightness.""" - device = _mock_device() - device.brightness = None - await _setup_mocked_entry(hass, device) - entity_id = "light.haiku_fan" - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - attributes = state.attributes - assert attributes[ATTR_BRIGHTNESS] == 255 - assert attributes[ATTR_COLOR_MODE] == ColorMode.BRIGHTNESS - assert attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.BRIGHTNESS] - - -async def test_standalone_light(hass: HomeAssistant) -> None: - """Test a standalone light.""" - device = _mock_device() - device.is_light = True - device.light_color_temp_max = 6500 - device.light_color_temp_min = 2700 - device.light_color_temp = 4000 - await _setup_mocked_entry(hass, device) - entity_id = "light.haiku_fan_light" - - state = hass.states.get(entity_id) - assert state.state == STATE_ON - attributes = state.attributes - assert attributes[ATTR_BRIGHTNESS] == 255 - assert attributes[ATTR_COLOR_MODE] == ColorMode.COLOR_TEMP - assert attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.COLOR_TEMP] - assert attributes[ATTR_COLOR_TEMP] == 250 - - await hass.services.async_call( - LIGHT_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True - ) - assert device.light_on is False - - await hass.services.async_call( - LIGHT_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True - ) - assert device.light_on is True