示例#1
0
文件: test_setup.py 项目: rikroe/core
async def test_when_setup_already_loaded(hass):
    """Test when setup."""
    calls = []

    async def mock_callback(hass, component):
        """Mock callback."""
        calls.append(component)

    setup.async_when_setup(hass, "test", mock_callback)
    await hass.async_block_till_done()
    assert calls == []

    hass.config.components.add("test")
    hass.bus.async_fire(EVENT_COMPONENT_LOADED, {"component": "test"})
    await hass.async_block_till_done()
    assert calls == ["test"]

    # Event listener should be gone
    hass.bus.async_fire(EVENT_COMPONENT_LOADED, {"component": "test"})
    await hass.async_block_till_done()
    assert calls == ["test"]

    # Should be called right away
    setup.async_when_setup(hass, "test", mock_callback)
    await hass.async_block_till_done()
    assert calls == ["test", "test"]
示例#2
0
async def async_setup_entry(hass, entry):
    """Set up OwnTracks entry."""
    config = hass.data[DOMAIN]['config']
    max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY)
    waypoint_import = config.get(CONF_WAYPOINT_IMPORT)
    waypoint_whitelist = config.get(CONF_WAYPOINT_WHITELIST)
    secret = config.get(CONF_SECRET) or entry.data[CONF_SECRET]
    region_mapping = config.get(CONF_REGION_MAPPING)
    events_only = config.get(CONF_EVENTS_ONLY)
    mqtt_topic = config.get(CONF_MQTT_TOPIC)

    context = OwnTracksContext(hass, secret, max_gps_accuracy,
                               waypoint_import, waypoint_whitelist,
                               region_mapping, events_only, mqtt_topic)

    webhook_id = config.get(CONF_WEBHOOK_ID) or entry.data[CONF_WEBHOOK_ID]

    hass.data[DOMAIN]['context'] = context

    async_when_setup(hass, 'mqtt', async_connect_mqtt)

    hass.components.webhook.async_register(
        DOMAIN, 'OwnTracks', webhook_id, handle_webhook)

    hass.async_create_task(hass.config_entries.async_forward_entry_setup(
        entry, 'device_tracker'))

    return True
示例#3
0
async def async_setup_entry(hass, entry):
    """Set up OwnTracks entry."""
    config = hass.data[DOMAIN]['config']
    max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY)
    waypoint_import = config.get(CONF_WAYPOINT_IMPORT)
    waypoint_whitelist = config.get(CONF_WAYPOINT_WHITELIST)
    secret = config.get(CONF_SECRET) or entry.data[CONF_SECRET]
    region_mapping = config.get(CONF_REGION_MAPPING)
    events_only = config.get(CONF_EVENTS_ONLY)
    mqtt_topic = config.get(CONF_MQTT_TOPIC)

    context = OwnTracksContext(hass, secret, max_gps_accuracy,
                               waypoint_import, waypoint_whitelist,
                               region_mapping, events_only, mqtt_topic)

    webhook_id = config.get(CONF_WEBHOOK_ID) or entry.data[CONF_WEBHOOK_ID]

    hass.data[DOMAIN]['context'] = context

    async_when_setup(hass, 'mqtt', async_connect_mqtt)

    hass.components.webhook.async_register(
        DOMAIN, 'OwnTracks', webhook_id, handle_webhook)

    hass.async_create_task(hass.config_entries.async_forward_entry_setup(
        entry, 'device_tracker'))

    hass.data[DOMAIN]['unsub'] = \
        hass.helpers.dispatcher.async_dispatcher_connect(
            DOMAIN, async_handle_message)

    return True
示例#4
0
async def test_when_setup_already_loaded(hass):
    """Test when setup."""
    calls = []

    async def mock_callback(hass, component):
        """Mock callback."""
        calls.append(component)

    setup.async_when_setup(hass, 'test', mock_callback)
    await hass.async_block_till_done()
    assert calls == []

    hass.config.components.add('test')
    hass.bus.async_fire(EVENT_COMPONENT_LOADED, {
        'component': 'test'
    })
    await hass.async_block_till_done()
    assert calls == ['test']

    # Event listener should be gone
    hass.bus.async_fire(EVENT_COMPONENT_LOADED, {
        'component': 'test'
    })
    await hass.async_block_till_done()
    assert calls == ['test']

    # Should be called right away
    setup.async_when_setup(hass, 'test', mock_callback)
    await hass.async_block_till_done()
    assert calls == ['test', 'test']
示例#5
0
文件: device.py 项目: jbouwh/core
    def async_setup_events(self):
        """Set up the device events."""

        if self.option_events:
            self.api.stream.connection_status_callback.append(
                self.async_connection_status_callback)
            self.api.enable_events(event_callback=self.async_event_callback)
            self.api.stream.start()

            if self.api.vapix.mqtt:
                async_when_setup(self.hass, MQTT_DOMAIN, self.async_use_mqtt)
示例#6
0
        async def start_platforms():
            await asyncio.gather(
                *(self.hass.config_entries.async_forward_entry_setup(
                    self.config_entry, platform) for platform in PLATFORMS))
            if self.option_events:
                self.api.stream.connection_status_callback.append(
                    self.async_connection_status_callback)
                self.api.enable_events(
                    event_callback=self.async_event_callback)
                self.api.stream.start()

                if self.api.vapix.mqtt:
                    async_when_setup(self.hass, MQTT_DOMAIN, self.use_mqtt)
示例#7
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up OwnTracks entry."""
    config = hass.data[DOMAIN]["config"]
    max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY)
    waypoint_import = config.get(CONF_WAYPOINT_IMPORT)
    waypoint_whitelist = config.get(CONF_WAYPOINT_WHITELIST)
    secret = config.get(CONF_SECRET) or entry.data[CONF_SECRET]
    region_mapping = config.get(CONF_REGION_MAPPING)
    events_only = config.get(CONF_EVENTS_ONLY)
    mqtt_topic = config.get(CONF_MQTT_TOPIC)

    context = OwnTracksContext(
        hass,
        secret,
        max_gps_accuracy,
        waypoint_import,
        waypoint_whitelist,
        region_mapping,
        events_only,
        mqtt_topic,
    )

    webhook_id = config.get(CONF_WEBHOOK_ID) or entry.data[CONF_WEBHOOK_ID]

    hass.data[DOMAIN]["context"] = context

    async_when_setup(hass, "mqtt", async_connect_mqtt)

    hass.components.webhook.async_register(
        DOMAIN, "OwnTracks", webhook_id, handle_webhook
    )

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    hass.data[DOMAIN]["unsub"] = hass.helpers.dispatcher.async_dispatcher_connect(
        DOMAIN, async_handle_message
    )

    return True
示例#8
0
async def async_setup(hass, config):
    """Set up the camera component."""
    component = hass.data[DOMAIN] = EntityComponent(_LOGGER, DOMAIN, hass,
                                                    SCAN_INTERVAL)

    prefs = CameraPreferences(hass)
    await prefs.async_initialize()
    hass.data[DATA_CAMERA_PREFS] = prefs

    hass.http.register_view(CameraImageView(component))
    hass.http.register_view(CameraMjpegStream(component))
    hass.components.websocket_api.async_register_command(
        WS_TYPE_CAMERA_THUMBNAIL, websocket_camera_thumbnail,
        SCHEMA_WS_CAMERA_THUMBNAIL)
    hass.components.websocket_api.async_register_command(ws_camera_stream)
    hass.components.websocket_api.async_register_command(websocket_get_prefs)
    hass.components.websocket_api.async_register_command(
        websocket_update_prefs)

    await component.async_setup(config)

    async def preload_stream(hass, _):
        for camera in component.entities:
            camera_prefs = prefs.get(camera.entity_id)
            if not camera_prefs.preload_stream:
                continue

            async with async_timeout.timeout(10):
                source = await camera.stream_source()

            if not source:
                continue

            request_stream(hass,
                           source,
                           keepalive=True,
                           options=camera.stream_options)

    async_when_setup(hass, DOMAIN_STREAM, preload_stream)

    @callback
    def update_tokens(time):
        """Update tokens of the entities."""
        for entity in component.entities:
            entity.async_update_token()
            hass.async_create_task(entity.async_update_ha_state())

    hass.helpers.event.async_track_time_interval(update_tokens,
                                                 TOKEN_CHANGE_INTERVAL)

    component.async_register_entity_service(SERVICE_ENABLE_MOTION,
                                            CAMERA_SERVICE_SCHEMA,
                                            "async_enable_motion_detection")
    component.async_register_entity_service(SERVICE_DISABLE_MOTION,
                                            CAMERA_SERVICE_SCHEMA,
                                            "async_disable_motion_detection")
    component.async_register_entity_service(SERVICE_TURN_OFF,
                                            CAMERA_SERVICE_SCHEMA,
                                            "async_turn_off")
    component.async_register_entity_service(SERVICE_TURN_ON,
                                            CAMERA_SERVICE_SCHEMA,
                                            "async_turn_on")
    component.async_register_entity_service(SERVICE_SNAPSHOT,
                                            CAMERA_SERVICE_SNAPSHOT,
                                            async_handle_snapshot_service)
    component.async_register_entity_service(
        SERVICE_PLAY_STREAM,
        CAMERA_SERVICE_PLAY_STREAM,
        async_handle_play_stream_service,
    )
    component.async_register_entity_service(SERVICE_RECORD,
                                            CAMERA_SERVICE_RECORD,
                                            async_handle_record_service)

    return True