Example #1
0
async def test_open_close_update(gogogate2api_mock,
                                 opp: OpenPeerPower) -> None:
    """Test open and close and data update."""
    def info_response(door_status: DoorStatus) -> GogoGate2InfoResponse:
        return GogoGate2InfoResponse(
            user="******",
            gogogatename="gogogatename0",
            model="",
            apiversion="",
            remoteaccessenabled=False,
            remoteaccess="abc123.blah.blah",
            firmwareversion="",
            apicode="",
            door1=GogoGate2Door(
                door_id=1,
                permission=True,
                name="Door1",
                gate=False,
                mode=DoorMode.GARAGE,
                status=door_status,
                sensor=True,
                sensorid=None,
                camera=False,
                events=2,
                temperature=None,
                voltage=40,
            ),
            door2=GogoGate2Door(
                door_id=2,
                permission=True,
                name=None,
                gate=True,
                mode=DoorMode.GARAGE,
                status=DoorStatus.UNDEFINED,
                sensor=True,
                sensorid=None,
                camera=False,
                events=0,
                temperature=None,
                voltage=40,
            ),
            door3=GogoGate2Door(
                door_id=3,
                permission=True,
                name=None,
                gate=False,
                mode=DoorMode.GARAGE,
                status=DoorStatus.UNDEFINED,
                sensor=True,
                sensorid=None,
                camera=False,
                events=0,
                temperature=None,
                voltage=40,
            ),
            outputs=Outputs(output1=True, output2=False, output3=True),
            network=Network(ip=""),
            wifi=Wifi(SSID="", linkquality="", signal=""),
        )

    expected_attributes = {
        "device_class": "garage",
        "door_id": 1,
        "friendly_name": "Door1",
        "supported_features": SUPPORT_CLOSE | SUPPORT_OPEN,
    }

    api = MagicMock(GogoGate2Api)
    api.async_activate.return_value = GogoGate2ActivateResponse(result=True)
    api.async_info.return_value = info_response(DoorStatus.OPENED)
    api.async_get_door_statuses_from_info.return_value = {
        1: DoorStatus.OPENED,
        2: DoorStatus.OPENED,
    }
    gogogate2api_mock.return_value = api

    config_entry = MockConfigEntry(
        domain=DOMAIN,
        source=SOURCE_USER,
        data={
            CONF_IP_ADDRESS: "127.0.0.1",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
        },
    )
    config_entry.add_to_opp(opp)

    assert opp.states.get("cover.door1") is None
    assert await opp.config_entries.async_setup(config_entry.entry_id)
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_OPEN
    assert dict(
        opp.states.get("cover.door1").attributes) == expected_attributes

    api.async_info.return_value = info_response(DoorStatus.CLOSED)
    api.async_get_door_statuses_from_info.return_value = {
        1: DoorStatus.CLOSED,
        2: DoorStatus.CLOSED,
    }
    await opp.services.async_call(
        COVER_DOMAIN,
        "close_cover",
        service_data={"entity_id": "cover.door1"},
    )
    api.async_get_door_statuses_from_info.return_value = {
        1: TransitionDoorStatus.CLOSING,
        2: TransitionDoorStatus.CLOSING,
    }
    async_fire_time_changed(opp, utcnow() + timedelta(hours=2))
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_CLOSING
    api.async_close_door.assert_called_with(1)

    async_fire_time_changed(opp, utcnow() + timedelta(seconds=10))
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_CLOSING

    api.async_info.return_value = info_response(DoorStatus.CLOSED)
    api.async_get_door_statuses_from_info.return_value = {
        1: DoorStatus.CLOSED,
        2: DoorStatus.CLOSED,
    }
    async_fire_time_changed(opp, utcnow() + timedelta(hours=2))
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_CLOSED

    api.async_info.return_value = info_response(DoorStatus.OPENED)
    api.async_get_door_statuses_from_info.return_value = {
        1: DoorStatus.OPENED,
        2: DoorStatus.OPENED,
    }
    await opp.services.async_call(
        COVER_DOMAIN,
        "open_cover",
        service_data={"entity_id": "cover.door1"},
    )
    api.async_get_door_statuses_from_info.return_value = {
        1: TransitionDoorStatus.OPENING,
        2: TransitionDoorStatus.OPENING,
    }
    async_fire_time_changed(opp, utcnow() + timedelta(hours=2))
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_OPENING
    api.async_open_door.assert_called_with(1)

    async_fire_time_changed(opp, utcnow() + timedelta(seconds=10))
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_OPENING

    api.async_info.return_value = info_response(DoorStatus.OPENED)
    api.async_get_door_statuses_from_info.return_value = {
        1: DoorStatus.OPENED,
        2: DoorStatus.OPENED,
    }
    async_fire_time_changed(opp, utcnow() + timedelta(hours=2))
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_OPEN

    api.async_info.return_value = info_response(DoorStatus.UNDEFINED)
    api.async_get_door_statuses_from_info.return_value = {
        1: DoorStatus.UNDEFINED,
        2: DoorStatus.UNDEFINED,
    }
    async_fire_time_changed(opp, utcnow() + timedelta(hours=2))
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_UNKNOWN

    api.async_info.return_value = info_response(DoorStatus.OPENED)
    api.async_get_door_statuses_from_info.return_value = {
        1: DoorStatus.OPENED,
        2: DoorStatus.OPENED,
    }
    await opp.services.async_call(
        COVER_DOMAIN,
        "close_cover",
        service_data={"entity_id": "cover.door1"},
    )
    await opp.services.async_call(
        COVER_DOMAIN,
        "open_cover",
        service_data={"entity_id": "cover.door1"},
    )
    api.async_get_door_statuses_from_info.return_value = {
        1: TransitionDoorStatus.OPENING,
        2: TransitionDoorStatus.OPENING,
    }
    async_fire_time_changed(opp, utcnow() + timedelta(hours=2))
    await opp.async_block_till_done()
    assert opp.states.get("cover.door1").state == STATE_OPENING
    api.async_open_door.assert_called_with(1)

    assert await opp.config_entries.async_unload(config_entry.entry_id)
    assert not opp.states.async_entity_ids(DOMAIN)
Example #2
0
async def test_sensor_update(gogogate2api_mock, hass: HomeAssistant) -> None:
    """Test data update."""

    bat_attributes = {
        "device_class": "battery",
        "door_id": 1,
        "friendly_name": "Door1 battery",
        "sensor_id": "ABCD",
        "state_class": "measurement",
        "unit_of_measurement": "%",
    }
    temp_attributes = {
        "device_class": "temperature",
        "door_id": 1,
        "friendly_name": "Door1 temperature",
        "sensor_id": "ABCD",
        "unit_of_measurement": "°C",
        "state_class": "measurement",
    }

    api = MagicMock(GogoGate2Api)
    api.async_activate.return_value = GogoGate2ActivateResponse(result=True)
    api.async_info.return_value = _mocked_gogogate_sensor_response(25, 7.0)
    gogogate2api_mock.return_value = api

    config_entry = MockConfigEntry(
        domain=DOMAIN,
        source=SOURCE_USER,
        data={
            CONF_IP_ADDRESS: "127.0.0.1",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
        },
    )
    config_entry.add_to_hass(hass)

    assert hass.states.get("cover.door1") is None
    assert hass.states.get("cover.door2") is None
    assert hass.states.get("cover.door3") is None
    assert hass.states.get("sensor.door1_battery") is None
    assert hass.states.get("sensor.door2_battery") is None
    assert hass.states.get("sensor.door3_battery") is None
    assert hass.states.get("sensor.door1_temperature") is None
    assert hass.states.get("sensor.door2_temperature") is None
    assert hass.states.get("sensor.door3_temperature") is None
    assert await hass.config_entries.async_setup(config_entry.entry_id)
    await hass.async_block_till_done()
    assert hass.states.get("cover.door1")
    assert hass.states.get("cover.door2")
    assert hass.states.get("cover.door3")
    assert hass.states.get("sensor.door1_battery").state == "25"
    assert dict(
        hass.states.get("sensor.door1_battery").attributes) == bat_attributes
    assert hass.states.get("sensor.door2_battery") is None
    assert hass.states.get("sensor.door2_battery") is None
    assert hass.states.get("sensor.door1_temperature").state == "7.0"
    assert (dict(hass.states.get("sensor.door1_temperature").attributes) ==
            temp_attributes)
    assert hass.states.get("sensor.door2_temperature") is None
    assert hass.states.get("sensor.door3_temperature") is None

    api.async_info.return_value = _mocked_gogogate_sensor_response(40, 10.0)
    async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
    await hass.async_block_till_done()
    assert hass.states.get("sensor.door1_battery").state == "40"
    assert hass.states.get("sensor.door1_temperature").state == "10.0"

    api.async_info.return_value = _mocked_gogogate_sensor_response(None, None)
    async_fire_time_changed(hass, utcnow() + timedelta(hours=2))
    await hass.async_block_till_done()
    assert hass.states.get("sensor.door1_battery").state == STATE_UNKNOWN
    assert hass.states.get("sensor.door1_temperature").state == STATE_UNKNOWN

    assert await hass.config_entries.async_unload(config_entry.entry_id)
    assert not hass.states.async_entity_ids(DOMAIN)