示例#1
0
async def test_parse_overlapping_homekit_json(hass):
    """Test migrating .homekit/pairings.json files when hk- exists too."""
    service = FakeService("public.hap.service.lightbulb")
    on_char = service.add_characteristic("on")
    on_char.value = 1

    accessory = Accessory("TestDevice", "example.com", "Test", "0001", "0.1")
    accessory.services.append(service)

    fake_controller = await setup_platform(hass)
    pairing = fake_controller.add([accessory])
    pairing.pairing_data = {"AccessoryPairingID": "00:00:00:00:00:00"}

    mock_listdir = mock.Mock()
    mock_listdir.return_value = ["hk-00:00:00:00:00:00", "pairings.json"]

    mock_path = mock.Mock()
    mock_path.exists.side_effect = [True, True]

    # First file to get loaded is .homekit/pairing.json
    read_data_1 = {"00:00:00:00:00:00": {"AccessoryPairingID": "00:00:00:00:00:00"}}
    mock_open_1 = mock.mock_open(read_data=json.dumps(read_data_1))

    # Second file to get loaded is .homekit/hk-00:00:00:00:00:00
    read_data_2 = {"AccessoryPairingID": "00:00:00:00:00:00"}
    mock_open_2 = mock.mock_open(read_data=json.dumps(read_data_2))

    side_effects = [mock_open_1.return_value, mock_open_2.return_value]

    discovery_info = {
        "name": "TestDevice",
        "host": "127.0.0.1",
        "port": 8080,
        "properties": {"md": "TestDevice", "id": "00:00:00:00:00:00", "c#": 1, "sf": 0},
    }

    flow = _setup_flow_handler(hass)

    pairing_cls_imp = (
        "homeassistant.components.homekit_controller.config_flow.IpPairing"
    )

    with mock.patch(pairing_cls_imp) as pairing_cls:
        pairing_cls.return_value = pairing
        with mock.patch("builtins.open", side_effect=side_effects):
            with mock.patch("os.path", mock_path):
                with mock.patch("os.listdir", mock_listdir):
                    result = await flow.async_step_zeroconf(discovery_info)

        await hass.async_block_till_done()

    assert result["type"] == "create_entry"
    assert result["title"] == "TestDevice"
    assert result["data"]["AccessoryPairingID"] == "00:00:00:00:00:00"
    assert flow.context == {
        "hkid": "00:00:00:00:00:00",
        "title_placeholders": {"name": "TestDevice"},
        "unique_id": "00:00:00:00:00:00",
    }
def create_motion_sensor_service():
    """Define motion characteristics as per page 225 of HAP spec."""
    service = FakeService("public.hap.service.sensor.motion")

    cur_state = service.add_characteristic("motion-detected")
    cur_state.value = 0

    return service
def create_smoke_sensor_service():
    """Define smoke sensor characteristics."""
    service = FakeService("public.hap.service.sensor.smoke")

    cur_state = service.add_characteristic("smoke-detected")
    cur_state.value = 0

    return service
示例#4
0
def create_temperature_sensor_service():
    """Define temperature characteristics."""
    service = FakeService("public.hap.service.sensor.temperature")

    cur_state = service.add_characteristic("temperature.current")
    cur_state.value = 0

    return service
def create_sensor_motion_service():
    """Define motion characteristics as per page 225 of HAP spec."""
    service = FakeService('public.hap.service.sensor.motion')

    cur_state = service.add_characteristic('motion-detected')
    cur_state.value = 0

    return service
示例#6
0
def create_light_level_sensor_service():
    """Define light level characteristics."""
    service = FakeService("public.hap.service.sensor.light")

    cur_state = service.add_characteristic("light-level.current")
    cur_state.value = 0

    return service
示例#7
0
def create_humidity_sensor_service():
    """Define humidity characteristics."""
    service = FakeService("public.hap.service.sensor.humidity")

    cur_state = service.add_characteristic("relative-humidity.current")
    cur_state.value = 0

    return service
示例#8
0
def create_humidity_sensor_service():
    """Define humidity characteristics."""
    service = FakeService('public.hap.service.sensor.humidity')

    cur_state = service.add_characteristic('relative-humidity.current')
    cur_state.value = 0

    return service
示例#9
0
def create_light_level_sensor_service():
    """Define light level characteristics."""
    service = FakeService('public.hap.service.sensor.light')

    cur_state = service.add_characteristic('light-level.current')
    cur_state.value = 0

    return service
示例#10
0
def create_temperature_sensor_service():
    """Define temperature characteristics."""
    service = FakeService('public.hap.service.sensor.temperature')

    cur_state = service.add_characteristic('temperature.current')
    cur_state.value = 0

    return service
def create_contact_sensor_service():
    """Define contact characteristics."""
    service = FakeService("public.hap.service.sensor.contact")

    cur_state = service.add_characteristic("contact-state")
    cur_state.value = 0

    return service
示例#12
0
def create_carbon_dioxide_level_sensor_service():
    """Define carbon dioxide level characteristics."""
    service = FakeService("public.hap.service.sensor.carbon-dioxide")

    cur_state = service.add_characteristic("carbon-dioxide.level")
    cur_state.value = 0

    return service
async def test_parse_old_homekit_json(hass):
    """Test migrating original .homekit/hk-00:00:00:00:00:00 files."""
    service = FakeService('public.hap.service.lightbulb')
    on_char = service.add_characteristic('on')
    on_char.value = 1

    accessory = Accessory('TestDevice', 'example.com', 'Test', '0001', '0.1')
    accessory.services.append(service)

    fake_controller = await setup_platform(hass)
    pairing = fake_controller.add([accessory])
    pairing.pairing_data = {
        'AccessoryPairingID': '00:00:00:00:00:00',
    }

    mock_path = mock.Mock()
    mock_path.exists.side_effect = [False, True]

    mock_listdir = mock.Mock()
    mock_listdir.return_value = ['hk-00:00:00:00:00:00', 'pairings.json']

    read_data = {
        'AccessoryPairingID': '00:00:00:00:00:00',
    }
    mock_open = mock.mock_open(read_data=json.dumps(read_data))

    discovery_info = {
        'name': 'TestDevice',
        'host': '127.0.0.1',
        'port': 8080,
        'properties': {
            'md': 'TestDevice',
            'id': '00:00:00:00:00:00',
            'c#': 1,
            'sf': 0,
        }
    }

    flow = _setup_flow_handler(hass)

    pairing_cls_imp = "homekit.controller.ip_implementation.IpPairing"

    with mock.patch(pairing_cls_imp) as pairing_cls:
        pairing_cls.return_value = pairing
        with mock.patch('builtins.open', mock_open):
            with mock.patch('os.path', mock_path):
                with mock.patch('os.listdir', mock_listdir):
                    result = await flow.async_step_zeroconf(discovery_info)

    assert result['type'] == 'create_entry'
    assert result['title'] == 'TestDevice'
    assert result['data']['AccessoryPairingID'] == '00:00:00:00:00:00'
    assert flow.context == {
        'hkid': '00:00:00:00:00:00',
        'title_placeholders': {
            'name': 'TestDevice'
        }
    }
示例#14
0
def create_air_quality_sensor_service():
    """Define temperature characteristics."""
    service = FakeService("public.hap.service.sensor.air-quality")

    cur_state = service.add_characteristic("air-quality")
    cur_state.value = 5

    cur_state = service.add_characteristic("density.ozone")
    cur_state.value = 1111

    cur_state = service.add_characteristic("density.no2")
    cur_state.value = 2222

    cur_state = service.add_characteristic("density.so2")
    cur_state.value = 3333

    cur_state = service.add_characteristic("density.pm25")
    cur_state.value = 4444

    cur_state = service.add_characteristic("density.pm10")
    cur_state.value = 5555

    cur_state = service.add_characteristic("density.voc")
    cur_state.value = 6666

    return service
示例#15
0
async def test_parse_old_homekit_json(hass):
    """Test migrating original .homekit/hk-00:00:00:00:00:00 files."""
    service = FakeService('public.hap.service.lightbulb')
    on_char = service.add_characteristic('on')
    on_char.value = 1

    accessory = Accessory('TestDevice', 'example.com', 'Test', '0001', '0.1')
    accessory.services.append(service)

    fake_controller = await setup_platform(hass)
    pairing = fake_controller.add([accessory])
    pairing.pairing_data = {
        'AccessoryPairingID': '00:00:00:00:00:00',
    }

    mock_path = mock.Mock()
    mock_path.exists.side_effect = [False, True]

    mock_listdir = mock.Mock()
    mock_listdir.return_value = [
        'hk-00:00:00:00:00:00',
        'pairings.json'
    ]

    read_data = {
        'AccessoryPairingID': '00:00:00:00:00:00',
    }
    mock_open = mock.mock_open(read_data=json.dumps(read_data))

    discovery_info = {
        'host': '127.0.0.1',
        'port': 8080,
        'properties': {
            'md': 'TestDevice',
            'id': '00:00:00:00:00:00',
            'c#': 1,
            'sf': 0,
        }
    }

    flow = config_flow.HomekitControllerFlowHandler()
    flow.hass = hass

    pairing_cls_imp = "homekit.controller.ip_implementation.IpPairing"

    with mock.patch(pairing_cls_imp) as pairing_cls:
        pairing_cls.return_value = pairing
        with mock.patch('builtins.open', mock_open):
            with mock.patch('os.path', mock_path):
                with mock.patch('os.listdir', mock_listdir):
                    result = await flow.async_step_discovery(discovery_info)

    assert result['type'] == 'create_entry'
    assert result['title'] == 'TestDevice'
    assert result['data']['AccessoryPairingID'] == '00:00:00:00:00:00'
示例#16
0
def create_lightbulb_service():
    """Define lightbulb characteristics."""
    service = FakeService("public.hap.service.lightbulb")

    on_char = service.add_characteristic("on")
    on_char.value = 0

    brightness = service.add_characteristic("brightness")
    brightness.value = 0

    return service
示例#17
0
async def test_climate_respect_supported_op_modes_2(hass, utcnow):
    """Test that climate respects validValue hints."""
    service = FakeService('public.hap.service.thermostat')
    char = service.add_characteristic('heating-cooling.target')
    char.value = 0
    char.validValues = [0, 1, 2]

    helper = await setup_test_component(hass, [service])

    state = await helper.poll_and_get_state()
    assert state.attributes['operation_list'] == ['off', 'heat', 'cool']
示例#18
0
async def test_climate_respect_supported_op_modes_2(hass, utcnow):
    """Test that climate respects validValue hints."""
    service = FakeService("public.hap.service.thermostat")
    char = service.add_characteristic("heating-cooling.target")
    char.value = 0
    char.valid_values = [0, 1, 2]

    helper = await setup_test_component(hass, [service])

    state = await helper.poll_and_get_state()
    assert state.attributes["hvac_modes"] == ["off", "heat", "cool"]
示例#19
0
def create_lightbulb_service():
    """Define lightbulb characteristics."""
    service = FakeService('public.hap.service.lightbulb')

    on_char = service.add_characteristic('on')
    on_char.value = 0

    brightness = service.add_characteristic('brightness')
    brightness.value = 0

    return service
async def test_parse_new_homekit_json(hass):
    """Test migrating recent .homekit/pairings.json files."""
    service = FakeService("public.hap.service.lightbulb")
    on_char = service.add_characteristic("on")
    on_char.value = 1

    accessory = Accessory("TestDevice", "example.com", "Test", "0001", "0.1")
    accessory.services.append(service)

    fake_controller = await setup_platform(hass)
    pairing = fake_controller.add([accessory])
    pairing.pairing_data = {"AccessoryPairingID": "00:00:00:00:00:00"}

    mock_path = mock.Mock()
    mock_path.exists.side_effect = [True, False]

    read_data = {"00:00:00:00:00:00": pairing.pairing_data}
    mock_open = mock.mock_open(read_data=json.dumps(read_data))

    discovery_info = {
        "name": "TestDevice",
        "host": "127.0.0.1",
        "port": 8080,
        "properties": {
            "md": "TestDevice",
            "id": "00:00:00:00:00:00",
            "c#": 1,
            "sf": 0
        },
    }

    flow = _setup_flow_handler(hass)

    pairing_cls_imp = (
        "homeassistant.components.homekit_controller.config_flow.IpPairing")

    with mock.patch(pairing_cls_imp) as pairing_cls:
        pairing_cls.return_value = pairing
        with mock.patch("builtins.open", mock_open):
            with mock.patch("os.path", mock_path):
                result = await flow.async_step_zeroconf(discovery_info)

    assert result["type"] == "create_entry"
    assert result["title"] == "TestDevice"
    assert result["data"]["AccessoryPairingID"] == "00:00:00:00:00:00"
    assert flow.context == {
        "hkid": "00:00:00:00:00:00",
        "title_placeholders": {
            "name": "TestDevice"
        },
        "unique_id": "00:00:00:00:00:00",
    }
示例#21
0
def create_battery_level_sensor():
    """Define battery level characteristics."""
    service = FakeService("public.hap.service.battery")

    cur_state = service.add_characteristic("battery-level")
    cur_state.value = 100

    low_battery = service.add_characteristic("status-lo-batt")
    low_battery.value = 0

    charging_state = service.add_characteristic("charging-state")
    charging_state.value = 0

    return service
示例#22
0
async def test_parse_new_homekit_json(hass):
    """Test migrating recent .homekit/pairings.json files."""
    service = FakeService('public.hap.service.lightbulb')
    on_char = service.add_characteristic('on')
    on_char.value = 1

    accessory = Accessory('TestDevice', 'example.com', 'Test', '0001', '0.1')
    accessory.services.append(service)

    fake_controller = await setup_platform(hass)
    pairing = fake_controller.add([accessory])
    pairing.pairing_data = {
        'AccessoryPairingID': '00:00:00:00:00:00',
    }

    mock_path = mock.Mock()
    mock_path.exists.side_effect = [True, False]

    read_data = {
        '00:00:00:00:00:00': pairing.pairing_data,
    }
    mock_open = mock.mock_open(read_data=json.dumps(read_data))

    discovery_info = {
        'host': '127.0.0.1',
        'port': 8080,
        'properties': {
            'md': 'TestDevice',
            'id': '00:00:00:00:00:00',
            'c#': 1,
            'sf': 0,
        }
    }

    flow = config_flow.HomekitControllerFlowHandler()
    flow.hass = hass

    pairing_cls_imp = "homekit.controller.ip_implementation.IpPairing"

    with mock.patch(pairing_cls_imp) as pairing_cls:
        pairing_cls.return_value = pairing
        with mock.patch('builtins.open', mock_open):
            with mock.patch('os.path', mock_path):
                result = await flow.async_step_discovery(discovery_info)

    assert result['type'] == 'create_entry'
    assert result['title'] == 'TestDevice'
    assert result['data']['AccessoryPairingID'] == '00:00:00:00:00:00'
示例#23
0
def create_garage_door_opener_service():
    """Define a garage-door-opener chars as per page 217 of HAP spec."""
    service = FakeService('public.hap.service.garage-door-opener')

    cur_state = service.add_characteristic('door-state.current')
    cur_state.value = 0

    targ_state = service.add_characteristic('door-state.target')
    targ_state.value = 0

    obstruction = service.add_characteristic('obstruction-detected')
    obstruction.value = False

    name = service.add_characteristic('name')
    name.value = "testdevice"

    return service
示例#24
0
def create_lock_service():
    """Define a lock characteristics as per page 219 of HAP spec."""
    service = FakeService('public.hap.service.lock-mechanism')

    cur_state = service.add_characteristic('lock-mechanism.current-state')
    cur_state.value = 0

    targ_state = service.add_characteristic('lock-mechanism.target-state')
    targ_state.value = 0

    # According to the spec, a battery-level characteristic is normally
    # part of a seperate service. However as the code was written (which
    # predates this test) the battery level would have to be part of the lock
    # service as it is here.
    targ_state = service.add_characteristic('battery-level')
    targ_state.value = 50

    return service
def create_security_system_service():
    """Define a security-system characteristics as per page 219 of HAP spec."""
    service = FakeService('public.hap.service.security-system')

    cur_state = service.add_characteristic('security-system-state.current')
    cur_state.value = 0

    targ_state = service.add_characteristic('security-system-state.target')
    targ_state.value = 0

    # According to the spec, a battery-level characteristic is normally
    # part of a seperate service. However as the code was written (which
    # predates this test) the battery level would have to be part of the lock
    # service as it is here.
    targ_state = service.add_characteristic('battery-level')
    targ_state.value = 50

    return service
示例#26
0
def create_lock_service():
    """Define a lock characteristics as per page 219 of HAP spec."""
    service = FakeService("public.hap.service.lock-mechanism")

    cur_state = service.add_characteristic("lock-mechanism.current-state")
    cur_state.value = 0

    targ_state = service.add_characteristic("lock-mechanism.target-state")
    targ_state.value = 0

    # According to the spec, a battery-level characteristic is normally
    # part of a seperate service. However as the code was written (which
    # predates this test) the battery level would have to be part of the lock
    # service as it is here.
    targ_state = service.add_characteristic("battery-level")
    targ_state.value = 50

    return service
示例#27
0
def create_fan_service():
    """
    Define fan v1 characteristics as per HAP spec.

    This service is no longer documented in R2 of the public HAP spec but existing
    devices out there use it (like the SIMPLEconnect fan)
    """
    service = FakeService("public.hap.service.fan")

    cur_state = service.add_characteristic("on")
    cur_state.value = 0

    cur_state = service.add_characteristic("rotation.direction")
    cur_state.value = 0

    cur_state = service.add_characteristic("rotation.speed")
    cur_state.value = 0

    return service
示例#28
0
def create_window_covering_service():
    """Define a window-covering characteristics as per page 219 of HAP spec."""
    service = FakeService("public.hap.service.window-covering")

    cur_state = service.add_characteristic("position.current")
    cur_state.value = 0

    targ_state = service.add_characteristic("position.target")
    targ_state.value = 0

    position_state = service.add_characteristic("position.state")
    position_state.value = 0

    position_hold = service.add_characteristic("position.hold")
    position_hold.value = 0

    obstruction = service.add_characteristic("obstruction-detected")
    obstruction.value = False

    name = service.add_characteristic("name")
    name.value = "testdevice"

    return service
示例#29
0
def create_thermostat_service():
    """Define thermostat characteristics."""
    service = FakeService('public.hap.service.thermostat')

    char = service.add_characteristic('heating-cooling.target')
    char.value = 0

    char = service.add_characteristic('heating-cooling.current')
    char.value = 0

    char = service.add_characteristic('temperature.target')
    char.value = 0

    char = service.add_characteristic('temperature.current')
    char.value = 0

    char = service.add_characteristic('relative-humidity.target')
    char.value = 0

    char = service.add_characteristic('relative-humidity.current')
    char.value = 0

    return service
示例#30
0
def create_garage_door_opener_service():
    """Define a garage-door-opener chars as per page 217 of HAP spec."""
    service = FakeService("public.hap.service.garage-door-opener")

    cur_state = service.add_characteristic("door-state.current")
    cur_state.value = 0

    targ_state = service.add_characteristic("door-state.target")
    targ_state.value = 0

    obstruction = service.add_characteristic("obstruction-detected")
    obstruction.value = False

    name = service.add_characteristic("name")
    name.value = "testdevice"

    return service
示例#31
0
def create_fanv2_service():
    """Define fan v2 characteristics as per HAP spec."""
    service = FakeService("public.hap.service.fanv2")

    cur_state = service.add_characteristic("active")
    cur_state.value = 0

    cur_state = service.add_characteristic("rotation.direction")
    cur_state.value = 0

    cur_state = service.add_characteristic("rotation.speed")
    cur_state.value = 0

    cur_state = service.add_characteristic("swing-mode")
    cur_state.value = 0

    return service
示例#32
0
def create_window_covering_service():
    """Define a window-covering characteristics as per page 219 of HAP spec."""
    service = FakeService('public.hap.service.window-covering')

    cur_state = service.add_characteristic('position.current')
    cur_state.value = 0

    targ_state = service.add_characteristic('position.target')
    targ_state.value = 0

    position_state = service.add_characteristic('position.state')
    position_state.value = 0

    position_hold = service.add_characteristic('position.hold')
    position_hold.value = 0

    obstruction = service.add_characteristic('obstruction-detected')
    obstruction.value = False

    name = service.add_characteristic('name')
    name.value = "testdevice"

    return service
示例#33
0
async def test_parse_overlapping_homekit_json(hass):
    """Test migrating .homekit/pairings.json files when hk- exists too."""
    service = FakeService('public.hap.service.lightbulb')
    on_char = service.add_characteristic('on')
    on_char.value = 1

    accessory = Accessory('TestDevice', 'example.com', 'Test', '0001', '0.1')
    accessory.services.append(service)

    fake_controller = await setup_platform(hass)
    pairing = fake_controller.add([accessory])
    pairing.pairing_data = {
        'AccessoryPairingID': '00:00:00:00:00:00',
    }

    mock_listdir = mock.Mock()
    mock_listdir.return_value = ['hk-00:00:00:00:00:00', 'pairings.json']

    mock_path = mock.Mock()
    mock_path.exists.side_effect = [True, True]

    # First file to get loaded is .homekit/pairing.json
    read_data_1 = {
        '00:00:00:00:00:00': {
            'AccessoryPairingID': '00:00:00:00:00:00',
        }
    }
    mock_open_1 = mock.mock_open(read_data=json.dumps(read_data_1))

    # Second file to get loaded is .homekit/hk-00:00:00:00:00:00
    read_data_2 = {
        'AccessoryPairingID': '00:00:00:00:00:00',
    }
    mock_open_2 = mock.mock_open(read_data=json.dumps(read_data_2))

    side_effects = [mock_open_1.return_value, mock_open_2.return_value]

    discovery_info = {
        'host': '127.0.0.1',
        'port': 8080,
        'properties': {
            'md': 'TestDevice',
            'id': '00:00:00:00:00:00',
            'c#': 1,
            'sf': 0,
        }
    }

    flow = config_flow.HomekitControllerFlowHandler()
    flow.hass = hass

    pairing_cls_imp = "homekit.controller.ip_implementation.IpPairing"

    with mock.patch(pairing_cls_imp) as pairing_cls:
        pairing_cls.return_value = pairing
        with mock.patch('builtins.open', side_effect=side_effects):
            with mock.patch('os.path', mock_path):
                with mock.patch('os.listdir', mock_listdir):
                    result = await flow.async_step_discovery(discovery_info)

        await hass.async_block_till_done()

    assert result['type'] == 'create_entry'
    assert result['title'] == 'TestDevice'
    assert result['data']['AccessoryPairingID'] == '00:00:00:00:00:00'