"""Testing Device operations."""

import json
import unittest.mock as mock

from click.testing import CliRunner
import homeassistant_cli.cli as cli


def test_device_list(default_devices) -> None:
    """Test Device List."""
    with mock.patch(
        'homeassistant_cli.remote.get_devices', return_value=default_devices
    ):

        runner = CliRunner()
        result = runner.invoke(
            cli.cli,
            ["--output=json", "device", "list"],
            catch_exceptions=False,
        )
        assert result.exit_code == 0

        data = json.loads(result.output)
        assert len(data) == 23


def test_device_list_filter(default_devices) -> None:
    """Test Device List."""
    with mock.patch(
        'homeassistant_cli.remote.get_devices', return_value=default_devices
    ):

        runner = CliRunner()
        result = runner.invoke(
            cli.cli,
            ["--output=json", "device", "list", "table"],
            catch_exceptions=False,
        )
        assert result.exit_code == 0

        data = json.loads(result.output)
        assert len(data) == 2
        assert data[0]['name'] == "Kitchen table left"
        assert data[1]['name'] == "Kitchen table right"


def test_device_assign(default_areas, default_devices) -> None:
    """Test basic device assign."""
    with mock.patch(
        'homeassistant_cli.remote.get_devices', return_value=default_devices
    ):
        with mock.patch(
            'homeassistant_cli.remote.get_areas', return_value=default_areas
        ):
            with mock.patch(
                'homeassistant_cli.remote.assign_area',
                return_value={'success': True},
            ):

                runner = CliRunner()
                result = runner.invoke(
                    cli.cli,
                    ["device", "assign", "Kitchen", "Kitchen table left"],
                    catch_exceptions=False,
                )
                print(result.output)
                assert result.exit_code == 0
                expected = (
                    "Successfully assigned 'Kitchen'"
                    " to 'Kitchen table left'\n"
                )
                assert result.output == expected

[((234, 319), 'unittest.mock.patch', 'mock.patch', (['"""homeassistant_cli.remote.get_devices"""'], {'return_value': 'default_devices'}), "('homeassistant_cli.remote.get_devices', return_value=default_devices\n    )\n", (244, 319), True, 'import unittest.mock as mock\n'), ((348, 359), 'click.testing.CliRunner', 'CliRunner', ([], {}), '()\n', (357, 359), False, 'from click.testing import CliRunner\n'), ((561, 586), 'json.loads', 'json.loads', (['result.output'], {}), '(result.output)\n', (571, 586), False, 'import json\n'), ((711, 796), 'unittest.mock.patch', 'mock.patch', (['"""homeassistant_cli.remote.get_devices"""'], {'return_value': 'default_devices'}), "('homeassistant_cli.remote.get_devices', return_value=default_devices\n    )\n", (721, 796), True, 'import unittest.mock as mock\n'), ((825, 836), 'click.testing.CliRunner', 'CliRunner', ([], {}), '()\n', (834, 836), False, 'from click.testing import CliRunner\n'), ((1047, 1072), 'json.loads', 'json.loads', (['result.output'], {}), '(result.output)\n', (1057, 1072), False, 'import json\n'), ((1325, 1410), 'unittest.mock.patch', 'mock.patch', (['"""homeassistant_cli.remote.get_devices"""'], {'return_value': 'default_devices'}), "('homeassistant_cli.remote.get_devices', return_value=default_devices\n    )\n", (1335, 1410), True, 'import unittest.mock as mock\n'), ((1434, 1510), 'unittest.mock.patch', 'mock.patch', (['"""homeassistant_cli.remote.get_areas"""'], {'return_value': 'default_areas'}), "('homeassistant_cli.remote.get_areas', return_value=default_areas)\n", (1444, 1510), True, 'import unittest.mock as mock\n'), ((1551, 1637), 'unittest.mock.patch', 'mock.patch', (['"""homeassistant_cli.remote.assign_area"""'], {'return_value': "{'success': True}"}), "('homeassistant_cli.remote.assign_area', return_value={'success':\n    True})\n", (1561, 1637), True, 'import unittest.mock as mock\n'), ((1708, 1719), 'click.testing.CliRunner', 'CliRunner', ([], {}), '()\n', (1717, 1719), False, 'from click.testing import CliRunner\n')]